MinGW 4.8.1 C++ 11线程支持

new*_*mer 16 gcc mingw g++ c++11

我从官方网站下载了MinGW版本:http://sourceforge.net/projects/mingw/files/并将其安装在我的Windows 7机器上.

运行g++ --version给了我g++.exe (GCC) 4.8.1,我相信GCC 4.8.1支持C++ 11功能,包括线程.

运行g++ -std=c++11 main.cpp成功编译以下程序.

//main.cpp
#include <memory>

int main() {
    std::unique_ptr<int> a(new int);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但运行g++ -std=c++11 main.cpp以下程序:

//main.cpp
#include <mutex>

int main() {
    std::mutex myMutex;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

给出错误:

main.cpp: In function `int main()`:
main.cpp:5:5: error: 'mutex' is not a member of 'std'
    std::mutex myMutex;
    ^
main.cpp:5:16: error: expected ';' before 'myMutex'
    std::mutex myMutex;
                ^
Run Code Online (Sandbox Code Playgroud)

好像<mutex>不受支持.编译器没有抱怨#include <mutex>所以我不知道为什么我会收到这个错误.

sla*_*vak 10

如果我理解得很好,mingw仍然不支持std线程,但有些mingw-w64版本支持它.幸运的是,您仍然可以使用此版本的mingw构建32位应用程序.

这是构建链接.

  • 澄清一下:使用pthread构建(预编译,或者如果从mingw-w64安装程序中选择pthread),可以开箱即用; 使用win32-thread构建,你需要使用meganz插件,如Alexander Vassilev的答案所述. (3认同)

Ale*_*lev 9

已经存在std :: thread和sync原语的本机win32实现:https: //github.com/meganz/mingw-std-threads 它是一个仅限头的库,应该适用于任何符合C++ 11的版本MinGW的.