MinGW的C ++多线程 [英] C++ Multithreading with MinGW

查看:176
本文介绍了MinGW的C ++多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用c ++进行多线程.我在Windows 10上使用MinGW g ++编译器(版本8.2.0).当我尝试使用直接从网站获得的代码将内置线程库与c ++一起使用时,出现错误:

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:错误:未在此范围内声明线程"线程th1(foo,3);^ ~~~~~ main.cpp:34:5:注意:'std :: thread'在头文件''中定义;您是否忘记了"#include"?main.cpp:5:1:+#include使用命名空间std;main.cpp:34:5:线程th1(foo,3);^ ~~~~~ main.cpp:38:11:错误:预期为';'在"th2"之前线程th2(thread_obj(),3);^ ~~~;main.cpp:49:11:错误:预期为';'在"th3"之前螺纹th3(f,3);^ ~~~;main.cpp:53:5:错误:在此范围内未声明'th1'th1.join();^ ~~ main.cpp:56:5:错误:未在此范围内声明"th2"th2.join();^ ~~ main.cpp:59:5:错误:未在此范围内声明'th3'th3.join();^ ~~

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根本不允许标准线程库?如果没有,那么替代使用的一个好的库是什么?

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?

推荐答案

您可以:

  • 安装mingw-w64 时选择"pthreads"选项或
  • 在安装mingw-w64时选择"Win32线程"选项,并安装一个附加的头文件包.
  • 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.

另一个不错的选择是通过MSYS2 安装您使用合适的配置进行了最新的构建.(在这种情况下为pthread).

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

请参见如果您不打算通过MSYS2使用此线程以获取更多信息.

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

这篇关于MinGW的C ++多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆