在Windows 10上使用C 中的线程(使用G 作为编译器)的问题

problems using threads in C++ on windows 10 (using g++ as compiler)

本文关键字:使用 编译器 问题 线程 Windows      更新时间:2023-10-16

我正在尝试创建一个线程并将其打印到终端。我遇到了一些问题,所以我决定使用其他人制作的非常简单的代码。

#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(给出以下错误:

.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"软件包,因为这些软件包没有安装,但这并没有任何区别。我还将以下参数添加到编译器:

g++ -std=c++14 -pthread .multiT.cpp

对于其他处理此问题的人:最简单的解决方案是下载mingw-64并改用其编译器。

然后使用-std=gnu++11-std=c++11参数来启用对ISO C 2011标准的支持,并且通过扩展线程支持线程(请注意:此支持目前是实验性的,尽管到目前为止还没有给我任何问题(。