问题 MinGW和std :: thread


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

#include <iostream>
#include <thread>

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

int main()
{
    std::thread t(test);
}

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

g++ -std=c++11 test.cpp -o test.exe

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

  1. MinGW的-建立: 线程的Win32
  2. MinGW的-建立: 线程POSIX
  3. MinGW的-W64: stdthread实验rubenvb
  4. MinGW的-W64: stdthread实验rubenvb 4.7

自GCC以来,1号不起作用 显然只支持 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

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.

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

我也试过手动链接 winpthread 和 pthread,但这也没有做任何事情。

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

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


4780
2018-03-30 13:10


起源

花在寻找解决方案上的时间,最后点2工作!谢谢! - Massimo


答案:


你忘了加入你的主题:

t.join();

10
2018-03-30 13:23



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


仅供参考,已有std :: thread和sync原语的本机win32实现。它是一个仅限标头的库,适用于任何符合C ++ 11标准的MinGW版本。 https://github.com/meganz/mingw-std-threads


3
2017-12-11 11:34



这不是对已经解决的问题的真正答案,因此评论会更适合。谢谢分享! - Lukas