简体   繁体   中英

Joinable thread in C++

http://www.cplusplus.com/reference/thread/thread/joinable/

A thread object is joinable if it represents a thread of execution .

A thread object is not joinable in any of these cases:

 if it was **default-constructed**. if it has been **moved from** (either constructing another thread object, or assigning to it). if either of its members join or detach has been called.
  • What is the meaning of default constructed, here?

  • W.R.T moved from - When we have to put threads in a vector, we may create thread objects outside and then move them in the vector. Is this one of the cases which this moved from is referring to?

  • After detach has been called once, can we never join it again?

What is the meaning of default constructed, here?

It means a std::thread which was constructed with no arguments, and therefore does not represent a thread (ie it is not "running"). See docs: https://en.cppreference.com/w/cpp/thread/thread/thread

W.R.T moved from - When we have to put threads in a vector, we may create thread objects outside and then move them in the vector. Is this one of the cases which this moved from is referring to?

Yes, you cannot join a thread after moving it into the vector. But you can of course join the new thread object inside the vector (where it was "moved to").

After detach has been called once, can we never join it again?

That's right, you cannot join a thread which has already been joined or detached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM