Waits for the termination of a specified thread and returns the exit status of the terminated thread.
#include <nks/thread.h> int NXThreadJoin ( NXThreadId_t wait_for, NXThreadId_t *departed_thread, void **status);
(IN) Specifies the ID of the thread to wait for. Set to 0 to specify any undetached sibling thread. If set to the ID of an undetached thread, NXThreadJoin waits for that specific thread to terminate. If set to the ID of the calling thread, NXThreadJoin returns NX_EDEADLK.
(OUT) Points to the location where the ID of the thread is returned.
(OUT) Points to the location where the application-defined status of the terminated thread is returned.
If successful, returns 0; otherwise, returns a nonzero error code:
Only joinable application threads can be joined by calling NXThreadJoin. Detached application threads, daemon threads, and anonymous threads cannot be waited for.
When a detached thread exits, it cleans up after itself automatically. However, when a joinable thread exits and a thread is not already waiting to join the exiting thread, the system maintains sufficient state to support subsequent calls to NXThreadJoin. If you do not call NXThreadJoin on an exited joinable thread, system resources are held up unnecessarily.
NXThreadJoin is useful for applications that manage contexts. Contexts that are passed to threads created without the NX_THR_BIND_CTX flag are not automatically destroyed when the thread exits. When NXThreadJoin returns successfully, the context hosted by the thread that was joined can be safely freed (assuming that other application threads do not attempt to host this context) or reused by your application.
NOTE:An application can use any scheme to manage contexts and use it to decide when it is safe to free or reuse a context.
For sample code, see ThrJoin.c.