Windows 10(使用g ++作为编译器)在C ++中使用线程的问题 [英] problems using threads in C++ on windows 10 (using g++ as compiler)
问题描述
我正在尝试创建一个线程,并使其在终端上打印一些内容.我遇到了一些问题,因此我决定采用别人编写的这段非常简单的代码,当我编译它时,出现以下错误,但其他在线用户似乎没有问题可以运行此代码.
I'm trying to create a thread and make it print something to the terminal. I was having some problems so I decided to take this very simple piece of code someone else made, when I compile it I get the errors listed below, but other people online seem to have no problem running this.
#include <iostream>
#include <thread>
using namespace std;
void hello_world()
{
cout << "Hello from thread!\n";
}
int main()
{
thread threadobj1(hello_world);
threadobj1.join();
return 0;
}
编译器(在Windows 10上为Mingw32-gcc-g ++-bin 8.2.0.3)出现以下错误:
The compiler (mingw32-gcc-g++-bin 8.2.0.3 on windows 10) gives the following errors:
.\multiT.cpp: In function 'int main()':
.\multiT.cpp:13:5: error: 'thread' was not declared in this scope
thread threadobj1(hello_world);
^~~~~~
.\multiT.cpp:13:5: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?.\multiT.cpp:3:1:
+#include <thread>
.\multiT.cpp:13:5:
thread threadobj1(hello_world);
^~~~~~
.\multiT.cpp:14:5: error: 'threadobj1' was not declared in this scope
threadobj1.join();
^~~~~~~~~~
.\multiT.cpp:14:5: note: suggested alternative: 'thread_local'
threadobj1.join();
^~~~~~~~~~
thread_local
我希望有人可以帮我弄清楚为什么这对我不起作用,错误说我应该包括,但是我显然已经这样做了,所以现在有点迷路了.我已经尝试安装"mingw32-pthreads-w32-dev"软件包,因为这些软件包尚未安装,但没有任何区别.我还向编译器添加了以下参数:
I was hoping someone might be able to help me figure out why this isn't working for me, the error says I should include , but I've clearly already done that so I'm a bit lost now. I've already tried to install the "mingw32-pthreads-w32-dev" packages as those weren't installed but that hasn't made any difference. I've also added the following arguments to the compiler:
g++ -std=c++14 -pthread .\multiT.cpp
推荐答案
For anyone else dealing with this problem: The simplest solution is to download mingw-64 and use their compiler instead.
然后使用-std=gnu++11
或-std=c++11
参数启用对ISO C ++ 2011标准的支持以及对线程的扩展支持(请注意:该支持目前处于试验阶段,尽管到目前为止还没有给我带来任何问题)
Then use the -std=gnu++11
or -std=c++11
arguments to enable support for the ISO C++ 2011 standard and by extension support for threads (do note: This support is currently experimental, though that has not given me any problems so far).
这篇关于Windows 10(使用g ++作为编译器)在C ++中使用线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!