Waiting for thread termination

One thread can suspend itself to wait for the termination of another thread with the thr_join(S) function

   int thr_join(
   	thread_t wait_for,
   	thread_t *departed,
   	void     **status
where the parameters have the following meaning:

The ID of the thread of interest, that is, the non-detached thread whose termination the caller will await. A (thread_t)0 indicates interest in the next non-detached thread to terminate (or one that has already terminated, but has not been joined), whatever its ID happens to be.

thr_join(S) will deposit the thread ID of the terminated thread at this address.

thr_join(S) will deposit at this address the value given as an argument by the terminated thread when it called thr_exit(S). That value should be the address at which the terminated thread left its return value (exit status).

If the thread of interest has already terminated, thr_join(S) will return immediately; otherwise, the calling thread will block.

If there is more than one thread waiting for the termination of some particular thread:

If a thread receives a catchable signal while blocked in thr_join(S):

The resources of a non-detached thread (for example, a stack allocated by the Threads Library) will not be fully recovered by the Threads Library until some other thread has called thr_join(S) and received the terminated thread's exit status.

NOTE: Beware of lingering zombies! Failing to recover the memory and resources associated with terminated threads can have a negative impact on performance.

Detached threads

If the programmer knows at thread creation time that no other thread will use thr_join(S) to wait for the new thread, the THR_DETACHED flag to thr_create(S) should be used. When a ``detached thread'' terminates, its resources may be recovered immediately. In fact, it is not valid to use thr_join(S) on a detached thread.

By default, new threads are not detached threads.

Next topic: Thread-specific data
Previous topic: Termination of the process

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005