0

why threa destructor calls terminate

Hi We know that C++ thread need to be joined or detached before main thread terminates. Non joined and non detached thread destructor calls terminate. Why so? Just wanted to understand the need of C++ implementation to call terminate ?

22nd Oct 2024, 10:11 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 ответов
+ 2
It's just a cleanup measure to free up system resources, just like deallocating any heap space that the process left allocated upon termination. It is presumed unintentional if threads are left unjoined upon termination. You can make it intentional and leave the thread running by detaching the thread before main terminates.
22nd Oct 2024, 3:39 PM
Brian
Brian - avatar
0
A big part of programming is cleaning up behind yourself. If you allocate memory for something that you don't need anymore, you should release that memory when done. If you don't clean up after yourself, the program takes up more and more memory over time. That will cause problems. Each language has it's own method to help clean up behind bad programming. Some better than others. The goal is the same. If you, as a programmer, don't clean up behind yourself then language features are designed to try and clean up behind you. If you spawn a process, make sure you terminate it. If you allocate data, make sure you free/delete it.
22nd Oct 2024, 4:08 PM
Jerry Hobby
Jerry Hobby - avatar