C++ Multithreading with MinGW

流过昼夜 提交于 2021-02-17 02:20:44

问题


I would like to experiment with multithreading with c++. I am using the MinGW g++ compiler (version 8.2.0) on Windows 10. When I try to use the builtin thread library with c++ using the code I got directly from a website, I get the error:

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(); ^~~

Is this an issue with my specific to my compiler, or does MinGW simply not allow for the standard threading library? If not, what is a good library to use is substitute?


回答1:


You can either:

  • Choose the "pthreads" option when installing mingw-w64, or
  • Choose the "Win32 threads" option when installing mingw-w64, and install an additional header file pack.

Another good option is to install via MSYS2 which always gives you the latest build with a suitable configuration. (pthreads in this case).

See this thread for more info if you intend to not go via MSYS2.




回答2:


When you download mingw, there is a win32 version and a POSIX version. Get the POSIX and it will come with the std::thread implementation.



来源:https://stackoverflow.com/questions/56743884/c-multithreading-with-mingw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!