Reputation: 89
What is the state of thread after it completes its execution.?
Is it destroyed immediately after its execution or is it destroyed with parent thread.?
Upvotes: 4
Views: 291
Reputation: 22157
The std::thread
object is different than a underlying thread of control (although they should map 1-on-1).
This separation is really important and it implies that std::thread
and thread of control can have different life duration. For example, if you create your std::thread
on the stack, you really need to call thread::detach
before your object go destroyed (if you don't destructor will call terminate ). Also, as Grizzly pointed out, you can call .join()
before your object destruction which will block until the execution of the thread has finished.
This also answers your question - std::thread
object is not destroyed after the thread is finished - it is behaving as every other C++ object - it will be destroyed when it goes out of the scope (or gets deleted
).
Upvotes: 4