Search code examples
c++static-librariesc++20

segmentation fault using static libraries with std::jthread (g++-10)


I'm developing a library that works with std::jthread (new in C++20) using g++ 10.0.1. The library works fine if I compile it using share libraries, but If I compile it with static libraries I got a segmentation fault at the end of the program (thread destructor). I have narrowed down my test case to a simple thread creation and join:

#include <iostream>
#include <thread>

int main(int argc, const char** argv) {
  auto t = std::jthread([]() { std::cout << "OK\n"; });
  t.join();
  return 0;
}

To compile I use:

g++-10 -o test --static -std=c++20 test.cc -lpthread 

And running it:

% ./test
zsh: segmentation fault (core dumped)  ./test

There any one has an idea what this problem could be?

Update:

Following @JérômeRichard suggested reference, I was able to compile and run my little test program without problem

g++-10 -o test --static -std=c++20 test.cc -lrt -pthread  -Wl,--whole-archive -lpthread -Wl,--no-whole-archive

However, changing the code to use request_stop instead of join the program is segmenting fault again (https://godbolt.org/z/obGN8Y).

#include <iostream>
#include <thread>

int main() {
    auto t = std::jthread([]{ std::cout << "OK" << std::endl; });
    t.request_stop();
}

Solution

  • The problem was reported to libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989) and a patch has been generated to solve the problem.