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
).
source share