使用 MinGW 的 C++ 多线程
C++ Multithreading with MinGW
我想用 C++ 试验多线程。我在 Windows10 上使用 MinGW g++ 编译器(版本 8.2.0)。当我尝试使用我直接从网站获得的代码将内置线程库与 c++ 一起使用时,出现错误:
main.cpp:34:5: error: 'thread' was not declared in this scope
thread th1(foo, 3);
^~~~~~ main.cpp:34:5: note: 'std::thread' is defined in header ''; did you forget to '#include '? main.cpp:5:1:
+#include using namespace std; main.cpp:34:5:
thread th1(foo, 3);
^~~~~~ main.cpp:38:11: error: expected ';' before 'th2'
thread th2(thread_obj(), 3);
^~~~
; main.cpp:49:11: error: expected ';' before 'th3'
thread th3(f, 3);
^~~~
; main.cpp:53:5: error: 'th1' was not declared in this scope
th1.join();
^~~ main.cpp:56:5: error: 'th2' was not declared in this scope
th2.join();
^~~ main.cpp:59:5: error: 'th3' was not declared in this scope
th3.join();
^~~
这是我的编译器特有的问题,还是 MinGW 根本不允许标准线程库?如果没有,有什么好的库可以替代?
您可以:
- 当installing mingw-w64或
时选择"pthreads"选项
- 安装mingw-w64时选择"Win32 threads"选项,并安装额外的头文件包
另一个不错的选择是 ,它始终为您提供具有合适配置的最新版本。 (在本例中为 pthreads)。
如果您不打算通过 MSYS2,请参阅 了解更多信息。
下载mingw时,有win32版本和POSIX版本。获取 POSIX,它将随 std::thread 实现一起提供。
我想用 C++ 试验多线程。我在 Windows10 上使用 MinGW g++ 编译器(版本 8.2.0)。当我尝试使用我直接从网站获得的代码将内置线程库与 c++ 一起使用时,出现错误:
main.cpp:34:5: error: 'thread' was not declared in this scope thread th1(foo, 3); ^~~~~~ main.cpp:34:5: note: 'std::thread' is defined in header ''; did you forget to '#include '? main.cpp:5:1: +#include using namespace std; main.cpp:34:5: thread th1(foo, 3); ^~~~~~ main.cpp:38:11: error: expected ';' before 'th2' thread th2(thread_obj(), 3); ^~~~ ; main.cpp:49:11: error: expected ';' before 'th3' thread th3(f, 3); ^~~~ ; main.cpp:53:5: error: 'th1' was not declared in this scope th1.join(); ^~~ main.cpp:56:5: error: 'th2' was not declared in this scope th2.join(); ^~~ main.cpp:59:5: error: 'th3' was not declared in this scope th3.join(); ^~~
这是我的编译器特有的问题,还是 MinGW 根本不允许标准线程库?如果没有,有什么好的库可以替代?
您可以:
- 当installing mingw-w64或 时选择"pthreads"选项
- 安装mingw-w64时选择"Win32 threads"选项,并安装额外的头文件包
另一个不错的选择是
如果您不打算通过 MSYS2,请参阅
下载mingw时,有win32版本和POSIX版本。获取 POSIX,它将随 std::thread 实现一起提供。