我正在学习使用线程,因此我试图利用线程类运行代码,以便可以同时运行函数。但是,尝试在终端终端中对其进行编译时,它说未声明线程及其对象t1。
threading.cpp:16:5: error: 'thread' was not declared in this scope
thread t1(task1, "Hello");
^~~~~~
threading.cpp:21:5: error: 't1' was not declared in this scope
t1.join();
我认为g ++不支持它,但我也将其支持c ++ 11的论据包括在内
g++ -std=c++11 threading.cpp
您知道该如何处理该错误吗?
(操作系统:Windows,gcc版本6.3.0)
下面提供了代码(来自网站的示例):
#include <string>
#include <iostream>
#include <thread>
using namespace std;
// The function we want to execute on the new thread.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
// Do other things...
// Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
答案 0 :(得分:0)
您知道该如何处理该错误吗?
您的代码compiles and runs fine with a recent version of GCC(coliru.com)。对于较旧的版本(例如,GCC 6)和-std=c++11
,情况也是如此。您的问题必须在其他地方: