bool joinable() const noexcept;
返回线程对象是否是joinable的。
如果是一个正在执行的线程,那么它是joinable的。
A thread object is not joinable in any of these cases:
下列任一情况都是非joinable
例子:
#include <iostream> #include <thread> #include <ctime> using namespace std; void delay(double sec) { time_t start_time, cur_time; // 变量声明 time(&start_time); do { time(&cur_time); }while((cur_time - start_time) < sec ); }; void show(int n){ cout<<"n="<<n<<endl; } thread returnThread(){ thread tt(show,10); return tt; } int main() { thread t(show,18); cout<<"t is joinable? "<<t.joinable()<<endl; thread t1(returnThread()); cout<<"t1 is joinable? "<<t1.joinable()<<endl; thread t2(show,3); cout<<"t2 is joinable? "<<t2.joinable()<<endl; t2.join(); cout<<"after t2.join(),t2 is joinable? "<<t2.joinable()<<endl; thread t3(show,5); cout<<"t3 is joinable? "<<t3.joinable()<<endl; t3.detach(); cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl; }运行结果:
运行完了
cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl;
true
if the thread is joinable.false
otherwise. |
|
Edit & Run
|
Joinable after construction: foo: false bar: true Joinable after joining: foo: false bar: false |
No-throw guarantee: never throws exceptions.
—————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-4
于GDUT
——————————————————————————————————————————————————————————————————