Original address: http://www.cplusplus.com/reference/thread/thread/joinable/Public member function<thread> std::thread::joinable
BOOL Joinable () const noexcept;
Check if joinableReturns Whether the thread object is
joinable.
Returns whether the thread object is joinable.
a thread object is joinable if it represents A thread of execution.
If it is an executing thread, then it is joinable.
A object is not joinable in any of thread these cases:
Any of the following is a non-joinable
- If it was default-constructed.
Constructed by the default constructor.
- If it has been moved from (either constructing another thread object, or assigning to it).
obtained by moving constructs.
- If either of its members join or have detach been called.
A join or Detach method is called.
Example:
#include <iostream> #include <thread> #include <ctime>using namespace std;void delay (double sec) { time_t start_time, cur_time;//variable declaration 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;}
Operation Result:
Find a magical thing with GDB debugging.
Finished running
cout<< "After T3.detach (), T3 is joinable? "<<t3.joinable () <<endl;
after that, like a stack of solutions, good magic, now do not know why? hold on first, next time.
ParametersNone
Return Value
true
If the thread is
joinable.
false
otherwise.
Example
123456789101112131415161718192021222324252627
|
//Example for thread::joinable#include <iostream>//Std::cout#include <thread>//Std::thread voidMythread () {//do stuff ...}intMain () {std::thread foo; Std::thread Bar (mythread); Std::cout <<"Joinable after construction:\n"<< Std::boolalpha; Std::cout <<"foo: "<< foo.joinable () <<‘\n‘; Std::cout <<"bar: "<< bar.joinable () <<‘\n‘;if(Foo.joinable ()) Foo.join ();if(Bar.joinable ()) Bar.join (); Std::cout <<"Joinable after joining:\n"<< Std::boolalpha; Std::cout <<"foo: "<< foo.joinable () <<‘\n‘; Std::cout <<"bar: "<< bar.joinable () <<‘\n‘;return0;}
|
Edit & Run |
Output (after 3 seconds):
Joinable after construction:foo: falsebar: trueJoinable after joining:foo: falsebar: false |
Data RacesThe object is accessed.
Exception Safety
No-throw Guarantee: never throws exceptions.
—————————————————————————————————————————————————————————————————
Write the wrong or bad place please a lot of guidance, you can leave a message or click on the top left email address to send me an e-mail, point out my errors and shortcomings, so that I modify, better share to everyone, thank you.
Reprint Please specify source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email:[email protected]
2014-9-4
From Gdut
——————————————————————————————————————————————————————————————————
C++11 thread::joinable (5)