6

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

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

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Stuart Barth
  • 301
  • 2
  • 10
  • 3
    Be careful, you're using the word "it" and sometimes it's hard to tell if you mean the thread itself or the std::thread object. – David Schwartz Feb 04 '16 at 12:16
  • 2
    A good way to launch a detached `thread`: `std::thread(f, a0, a1, ...).detach();` No need to new it. No need to delete it. No chance of forgetting to `detach()` it. No chance of an exception getting thrown between your construction and the point of detach. – Howard Hinnant Feb 04 '16 at 18:04
  • @DavidSchwartz not sure I understand - why are a thread and a std::thread object different things? – Stuart Barth Feb 05 '16 at 01:54
  • @StuartBarth A thread is an execution vehicle that may or may not have a corresponding std::thread object. A std::thread object is an instance of a C++ class that may or may not have a corresponding thread. For example, you say "does the thread effectively delete itself upon termination". What does that mean? Are you asking if the thread deletes the std::thread object when the thread terminates? Clearly it can't, since that would cause a disaster if the thread that constructed the std::thread accessed the object if the thread terminated. So is that what you're asking? – David Schwartz Feb 07 '16 at 09:24

2 Answers2

7

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.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    I do not understand this `So the thread won't and impossible to delete it upon termination.`. Do you mean `The thread will not be deleted after terminating its execution and ...` What is impossible to delete upon termination? You mean I cannot delete the thread after it terminates? How can I know when the thread was terminated? How could I program the thread to delete itself after it terminates? – Evandro Coan Apr 24 '21 at 00:52
  • Can I program the thread to delete itself after it terminates by not creating the thread with `new`, then, I can just call `std::thread(...).detach()` and the thread will be automatically deallocated upon its termination? (as the documentation says: https://en.cppreference.com/w/cpp/thread/thread/detach `Separates the thread of execution from the thread object, allowing execution to continue independently. Any allocated resources will be freed once the thread exits.`) – Evandro Coan Apr 24 '21 at 01:04
  • 1
    @user I meant to `delete` the thread object. e.g. for `std::thread * t = new std::thread(...); t->detach();`, then you have to write `delete t;` manually. The thread being separated and executed from the thread object can't do that. – songyuanyao Apr 24 '21 at 01:15
  • Thanks! If I understand correctly, after calling `std::thread * t = new std::thread(...); t->detach();` I can just delete the thread with `delete t;` even if the thread still running, because the thread is now out of my application control, but with the Operating System handling its resources allocations/deallocations, until the thread exits and it is completely deleted by the Operating System. – Evandro Coan Apr 24 '21 at 01:23
  • So if you create a thread with local scope by just saying std::thread t = std:thread(...);, and then detach it, when the function exits, t will be deleted, but the detached thread will proceed just fine, right? As long as there are no local scope captures.. – Shavais Jun 14 '21 at 08:29
  • @Shavais Yes. This is exactly [the example from cppreference.com](https://en.cppreference.com/w/cpp/thread/thread/detach#Example) shows. – songyuanyao Jun 14 '21 at 12:42
2

Every object in C++ allocated using new must be released using delete.

Thread is an object "located within OS" (usually). It is created using std::thread constructor and released with detach().

Object of class std::thread is a С++-object, associated with the thread.

So you have to release both - OS-object and C++-object.


Upd. When you create thread, OS allocates internal structures within kernel space to control it. There are set of properties associated with each thread like state (running, pending, waiting resource), priority, return code, etc.

Victor Dyachenko
  • 1,363
  • 8
  • 18