如何使用posix线程在Windows上使用std ::线程
一直在尝试编译并运行以下非常简单的代码。
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
我一直在尝试使用MingW 6.3.0(Target X86_64-W64-MingW32)和线程模型(使用 g ++ -v )是posix。
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe
, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve找不到
)。
我还尝试使用 -pthread
或 -lpthread
标志来编译,但是我遇到了相同的运行时错误。
显然,这似乎与使用std ::线程有关,但是我没有得到解决方案。我是否缺少汇编标志或其他库,从而在Windows上启用POSIX线程支持?
编辑:我设法通过将汇编命令更改为
g ++ -wall -g test.cpp -o test.exe -static -libgcc -static -libstdc ++ -wl,-bstatic -lstdc ++ -lpthread -lpthread -wl,bdynamic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进行必要的修复后,我尝试了您的代码:
它运行良好。
构建命令与您的相同:
输出是:
但是我确实使用了mingw-w64 gcc 12.1.0(for ),而不是非常旧的6.3.0版。
也许您正在使用的Mingw-w64(还是它仍然是旧的mingw库)太老了...
I tried your code after making the necessary fixes:
and it worked fine.
The build command was the same as yours:
The output was:
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...