C ++ 11 Multithreading: thread state after execution

What is the state of the thread after completion of its execution?

It is destroyed immediately after its execution or destroyed by the parent thread.?

+4
source share
1 answer

The std::thread object is different from the main control thread (although they should display 1-by-1).

This separation is really important, and that means std::thread and control thread can have different lifespan. For example, if you create your std::thread on the stack, you really need to call thread::detach before your object is destroyed (unless you call terminate on the destructor). In addition, as Grizzlies pointed out, you can call .join() before destroying your object, which will block until the thread completes.

This also answers your question - the std::thread object is not destroyed after the thread ends - it behaves like any other C ++ object - it will be destroyed when it goes outside the scope (or gets deleted ).

+4
source

Source: https://habr.com/ru/post/1502780/


All Articles