https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98098

            Bug ID: 98098
           Summary: std::call_once throws std::system_error in statically
                    linked executable
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: g.bonacci at libero dot it
  Target Milestone: ---

This program, based on std::call_once,

    #include <thread>
    #include <mutex>
    #include <iostream>

    int count = 99;

    void f() { count++; }

    int main() {
        std::once_flag once;
        std::call_once(once, f);
        std::call_once(once, f);
        std::cout << count << '\n';
    }

fails when linked statically:

$ c++ -pthread -static  -std=c++11 -Wall -Wextra call-once.cc ; ./a.out 
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted

As far as I can tell, what triggers the error is this function called by
std::call_once:

static inline int
__gthread_active_p (void)
{
  static void *const __gthread_active_ptr
    = __extension__ (void *) &GTHR_ACTIVE_PROXY;
  return __gthread_active_ptr != 0;
}

where GTHR_ACTIVE_PROXY is 0 when statically linked, and a pointer to
__GI___pthread_key_create when dynamically linked.

A similar program based on pthread_once():

    #include <iostream>
    #include <pthread.h>

    int count = 99;

    void f() { count++; }

    int main() {
        pthread_once_t once = PTHREAD_ONCE_INIT;
        pthread_once(&once, f);
        pthread_once(&once, f);
        std::cout << count << '\n';
    }

works as expected when linked statically:

$ c++ -pthread -static -std=c++11 -Wall -Wextra pthread-once.cc; ./a.out
100

Best regards,
  g.b

Reply via email to