MinGW和std :: thread

Luk*_*kas 11 c++ windows mingw c++11 stdthread

所以我一直在尝试使用MinGW编译器在Windows上编译和运行以下代码.

#include <iostream>
#include <thread>

void test()
{
    std::cout << "test" << std::endl;
}

int main()
{
    std::thread t(test);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下命令进行编译:

g++ -std=c++11 test.cpp -o test.exe
Run Code Online (Sandbox Code Playgroud)

现在问题是应该使用的MinGW版本,我已经尝试过我所知道的所有版本.

  1. MinGW-builds:thread-win32
  2. MinGW-builds:thread-posix
  3. MinGW-w64:stdthread实验rubenvb
  4. MinGW-w64:stdthread实验rubenvb 4.7

数字1不起作用,因为GCC 显然只在内部支持 pthread.

数字2确实编译,它基本上甚至输出test(参见输出的最后一行),但它也崩溃了错误:

terminate called without an active exception

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
test
Run Code Online (Sandbox Code Playgroud)

数字3和4再次进行编译,但它们不输出test而是立即崩溃,但具有更具描述性的输出:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)

谷歌当然把我带到了GCC bug跟踪器和其他一些建议使用的帖子,这些帖子-pthread根本没用.

我也试过手动链接winpthreadpthread,但这也没有做任何事情.

-std=c++11-std=gnu++11... 之间也没有区别

我现在真的迷路了,不知道,如果它有可能得到一个支持的MinGW版本std::thread,但也许我只是忽略了一些编译器标志.我希望有人可以帮助我!

Ker*_* SB 10

你忘了加入你的主题:

t.join();
Run Code Online (Sandbox Code Playgroud)

  • 销毁可连接线程会导致抛出异常.由于您没有捕获异常,程序将终止. (3认同)
  • 好吧,我想我现在要杀了自己......但为什么要回到这样一个神秘的错误呢?它仍然不适用于所有版本,但至少有一个版本.谢谢! (2认同)