使用带有 std::jthread (g++-10) 的静态库的分段错误
segmentation fault using static libraries with std::jthread (g++-10)
我正在使用 g++ 10.0.1 开发一个与 std::jthread
(C++20 中的新功能)一起工作的库。如果我使用共享库编译它,该库工作正常,但如果我使用静态库编译它,我在程序结束时遇到分段错误(线程析构函数)。我已将我的测试用例缩小到一个简单的线程创建并加入:
#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();
}
已将问题报告给 libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989),并已生成补丁解决问题。
我正在使用 g++ 10.0.1 开发一个与 std::jthread
(C++20 中的新功能)一起工作的库。如果我使用共享库编译它,该库工作正常,但如果我使用静态库编译它,我在程序结束时遇到分段错误(线程析构函数)。我已将我的测试用例缩小到一个简单的线程创建并加入:
#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();
}
已将问题报告给 libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989),并已生成补丁解决问题。