我将其作为多线程示例的简化版本编写以了解它,但在编译时遇到了一些问题。我的编译器说它thread不是 的成员std并提示我添加#include <thread>到我的代码中,即使我已经这样做了。到目前为止,我一直无法找到任何类似的问题,但我怀疑这是我编译它的方式的问题,因为我的代码与示例代码非常相似。
#include <iostream>
#include <thread>
void doWork () {
std::cout << "Working...\n";
}
int main () {
std::thread worker(doWork);
work.join();
std::cout << "Finished\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器是MinGW的G ++ 9.2.0
我编译g++ main.cpp -o main它给了我这些错误:
main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
9 | std::thread worker(doWork);
| ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include …Run Code Online (Sandbox Code Playgroud)