Stuart Barth Stuart Barth - 1 year ago 267
C++ Question

Does a detached std::thread need to be deleted after it terminates?

I create a

std::thread object, and then
it. The thread runs for an arbitrary amount of time, and then terminates itself. Since I created the object with
, do I need to
it at some point to free up its resources? Or does the thread effectively
itself upon termination?

If it does effectively
itself, will something bad happen if I explicitly
it after it has terminated?

Answer Source

Yes, you have to delete it by yourself.

Once you called std::thread::detach, the thread will be separated from the thread object and allowed execution to continue independently, and then the thread object will no longer owns any thread. So the thread won't and impossible to delete it upon termination.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download