在尝试编译以下代码时
#include <thread>
#include <iostream>
void foo() { std::cout << "foo\n"; }
int main()
{
std::thread t(foo);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
C:\Test>g++ -g -Wall -lpthread -std=c++0x
main.cpp
main.cpp: In function 'int main()':
main.cpp:12:2: error: 'thread' is not a member of 'std'
main.cpp:12:14: error: expected ';' before 't'
main.cpp:13:2: error: 't' has not been declared
Run Code Online (Sandbox Code Playgroud)
如何使用C++ 11实验并发功能?我有MinGW GCC 4.5.1(TDM)
编辑: BTW,Visual Studio 2012执行良好的代码示例.
我希望这个代码在gcc下编译,但它没有.我正在使用mingw 4.7.0.我看到标题,但类似乎不存在.我需要什么旗帜?我跑了
g++ -std=gnu++11 main.cpp
Run Code Online (Sandbox Code Playgroud)
代码
#include<atomic>
#include<thread>
#include<iostream>
using namespace std;
atomic<int> a1,a2,a3;
void test(){
cout<<"run";
}
int main(){
thread t(test);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是线程不存在.在我的其他代码中它也说std :: this_thread :: yield()不存在.MSVC11编译这个罚款