我正在开发一个库,它可以使用std::jthread (在C++20中新建)使用g++ 10.0.1。如果我使用共享库编译它,这个库可以正常工作,但是如果我用静态库编译它,那么在程序的末尾就会出现分段错误(线程析构函数)。我已经将测试用例缩小为一个简单的线程创建和连接:
#include <iostream>
#include <thread>
int main(int argc, const char** argv) {
auto t = std::jthread([]() { std::cout << "OK\n"; });
t.join();
return 0;
}要编译,我使用:
g++-10 -o test --static -std=c++20 test.cc -lpthread 并运行它:
% ./test
zsh: segmentation fault (core dumped) ./test有人知道这个问题会是什么吗?
更新:
在@Jér meRichard建议引用之后,我能够毫无问题地编译和运行我的小测试程序。
g++-10 -o test --static -std=c++20 test.cc -lrt -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive但是,将代码更改为使用request_stop而不是join,程序将再次分割故障(https://godbolt.org/z/obGN8Y)。
#include <iostream>
#include <thread>
int main() {
auto t = std::jthread([]{ std::cout << "OK" << std::endl; });
t.request_stop();
}发布于 2020-11-11 18:15:41
向libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989)报告了该问题,并生成了解决该问题的修补程序。
https://stackoverflow.com/questions/62628884
复制相似问题