The GNU C Library implements a set of functions that allow the user to easily create and use threads. Additional functionality is provided to control the behavior of threads.
The following data types are defined for managing threads:
This data type is an
int (*) (void *)typedef that is passed tothrd_createwhen creating a new thread. It should point to the first function that thread will run.
The following functions are used for working with threads:
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_createcreates a new thread that will execute the function func. The object pointed to by arg will be used as the argument to func. If successful, thr is set to the new thread identifier.This function may return
thrd_success,thrd_nomem, orthrd_error.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This function returns the identifier of the calling thread.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_equalchecks whether lhs and rhs refer to the same thread. If lhs and rhs are different threads, this function returns 0; otherwise, the return value is non-zero.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_sleepblocks the execution of the current thread for at least until the elapsed time pointed to by time_point has been reached. This function does not take an absolute time, but a duration that the thread is required to be blocked. See Time Basics, and Elapsed Time.The thread may wake early if a signal that is not ignored is received. In such a case, if
remainingis not NULL, the remaining time duration is stored in the object pointed to by remaining.
thrd_sleepreturns 0 if it blocked for at least the amount of time intime_point, -1 if it was interrupted by a signal, or a negative number on failure.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_yieldprovides a hint to the implementation to reschedule the execution of the current thread, allowing other threads to run.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_exitterminates execution of the calling thread and sets its result code to res.If this function is called from a single-threaded process, the call is equivalent to calling
exitwithEXIT_SUCCESS(see Normal Termination). Also note that returning from a function that started a thread is equivalent to callingthrd_exit.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_detachdetaches the thread identified bythrfrom the current control thread. The resources held by the detached thread will be freed automatically once the thread exits. The parent thread will never be notified by any thr signal.Calling
thrd_detachon a thread that was previously detached or joined by another thread results in undefined behavior.This function returns either
thrd_successorthrd_error.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
thrd_joinblocks the current thread until the thread identified bythrfinishes execution. Ifresis not NULL, the result code of the thread is put into the location pointed to by res. The termination of the thread synchronizes-with the completion of this function, meaning both threads have arrived at a common point in their execution.Calling
thrd_joinon a thread that was previously detached or joined by another thread results in undefined behavior.This function returns either
thrd_successorthrd_error.