【发布时间】:2015-11-04 12:19:05
【问题描述】:
使用std::thread 运行一个无限循环的方法,有没有办法让循环查询线程是否已被请求加入...或者我是否必须手动添加一个“exitThread”标志?
换句话说,isJoined 会是什么样子(未经测试的伪代码):
std::atomic<int> global_counter (0);
void Run()
{
while(!isJoined())
{
doSomething();
++global_counter;
}
}
int main()
{
thread t(Run);
Sleep(10000);
t.join();
cout << "Iterated " << global_counter << "times" << endl;
}
【问题讨论】:
标签: c++ multithreading c++11 stl