Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When thread release its resources

In man page of pthread_detach i read that when any thread is detached then at a time of thread termination it release its resources back to system.

What are thread resources?Is that it is a part of memory used by that thread, if it is so then that memory is a part of a process address space. I am trying to understand this but i dint got it.

And what about the joinable thread, when does thread release its resources? at a time of pthread_join or at a time of termination or process?

When resources are released in pthread_cancel command.

like image 920
Abhitesh khatri Avatar asked Mar 14 '26 07:03

Abhitesh khatri


1 Answers

Every thread consumes some amount of bookkeeping resources in the operating system, as well as its own execution stack in userspace memory. Those resources are freed when the thread is destroyed, which can occur under several conditions, such as:

  • pthread_join returns when called on a joinable thread,
  • a detached thread's entry function returns,
  • main returns normally or exit is called,
  • the process is terminated due to receiving an unhandled signal,
  • exec is called successfully.

It is possible, however, to exit the thread that is running main and leave other, detached threads running. To do so, you have to call pthread_exit in the main thread.

like image 157
Kerrek SB Avatar answered Mar 16 '26 20:03

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!