所以我一直试图通过使用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的版本,我已经尝试了所有我知道的版本。
Number 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
编号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.
Google当然把我带到了GCC的bug跟踪器和其他一些post,build议使用-pthread
,根本没有任何帮助。
我也尝试手动链接到winpthread
和pthread
,但是这也没有做任何事情。
-std=c++11
和-std=gnu++11
之间也没有区别
我现在真的迷路了,不知道,如果完全可以得到一个支持std::thread
的MinGW版本,也许我只是忽略了一些编译器标志。 我希望有人能帮助我!
你忘了加入你的线程:
t.join();
仅供参考,已经有一个原生的win32实现std :: thread和sync原语。 它是一个仅包含头文件的库,适用于MinGW的任何C ++ 11兼容版本。 https://github.com/meganz/mingw-std-threads