NXThreadJoin

Waits for the termination of a specified thread and returns the exit status of the terminated thread.

Library:LibC
Classification:NKS
Service:Threads

Syntax

  #include <nks/thread.h>
   
  int NXThreadJoin (
     NXThreadId_t    wait_for,
     NXThreadId_t   *departed_thread, 
     void          **status);
  

Parameters

wait_for

(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.

departed_thread

(OUT) Points to the location where the ID of the thread is returned.

status

(OUT) Points to the location where the application-defined status of the terminated thread is returned.

Return Values

If successful, returns 0; otherwise, returns a nonzero error code:

Decimal

Hex

Constant

Description

9

0x09

NX_EINVAL

The wait_for thread is not joinable.

15

0x0F

NX_EDEADLK

The calling thread passed its own ID in wait_for.

77

0x4D

NX_ESRCH

The specified thread ID is invalid

Remarks

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.

See Also