Abhitesh khatri
Abhitesh khatri

Reputation: 3057

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.

Upvotes: 3

Views: 2438

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 3

Related Questions