Names specified here
Name Description Notes Source Availability
__STDC_NO_THREADS__ Indicator of lack of standard threading L ? M Predefined C11
Names specified for <threads.h>, “Threads”
Name Description Notes Source Availability
call_once() Ensure that a function has been invoked exactly once ? (·) <threads.h> C11
cnd_broadcast() Signal to all threads on a condition variable ? (·) <threads.h> C11
cnd_destroy() Destroy a condition variable ? (·) <threads.h> C11
cnd_init() Initialize a condition variable ? (·) <threads.h> C11
cnd_signal() Signal to a thread on a condition variable ? (·) <threads.h> C11
cnd_t Condition-variable type ? T <threads.h> C11
cnd_timedwait() Wait on a condition variable for a limited time ? (·) <threads.h> C11
cnd_wait() Wait on a condition variable ? (·) <threads.h> C11
mtx_destroy() Destroy a mutex ? (·) <threads.h> C11
mtx_init() Initialize a mutex ? (·) <threads.h> C11
mtx_lock() Lock a mutex ? (·) <threads.h> C11
mtx_plain Type of non-recursive, non-timeout mutex ? <threads.h> C11
mtx_recursive Type of mutex supporting recursion ? <threads.h> C11
mtx_t Mutex-identifier type ? T <threads.h> C11
mtx_timed Type of mutex supporting timeout ? <threads.h> C11
mtx_timedlock() Try for some time to lock a mutex ? (·) <threads.h> C11
mtx_trylock() Try to lock a mutex ? (·) <threads.h> C11
mtx_unlock() Unlock a mutex ? (·) <threads.h> C11
ONCE_FLAG_INIT Initializer for once_flag ? M <threads.h> C11
once_flag Flag type to detect exactly one invocation ? T <threads.h> C11
TSS_DTOR_ITERATIONS Maximum number of destructor invocations ? M <threads.h> C11
thrd_busy Indicator of blocked thread operation ? <threads.h> C11
thrd_create() Create and start a thread ? (·) <threads.h> C11
thrd_current() Identify the current thread ? (·) <threads.h> C11
thrd_detach() Dispose of thread resources ? (·) <threads.h> C11
thrd_equal() Test whether two thread identifiers refer to the same thread ? (·) <threads.h> C11
thrd_error Indicator of errant thread operation ? <threads.h> C11
thrd_exit() Terminate the current thread (·) <threads.h> C11
thrd_join() Await thread termination ? (·) <threads.h> C11
thrd_nomem Indicator of insufficient memory to carry out thread operation ? <threads.h> C11
thrd_sleep() Suspend current thread ? (·) <threads.h> C11
thrd_start_t Thread action type ? T <threads.h> C11
thrd_success Indicator of successful thread operation ? <threads.h> C11
thrd_t Thread-identifier type ? T <threads.h> C11
thrd_timedout Indicator of time-limited thread operation ? <threads.h> C11
thrd_yield() Allow other threads to run ? (·) <threads.h> C11
thread_local Storage class for thread-local variables ? M S <threads.h> C11
tss_create() Create thread-specific storage ? (·) <threads.h> C11
tss_delete() Destroy thread-specific storage ? (·) <threads.h> C11
tss_dtor_t Destructor type for thread-specific storage ? T <threads.h> C11
tss_get() Read thread-specific storage ? (·) <threads.h> C11
tss_set() Write thread-specific storage ? (·) <threads.h> C11
tss_t Thread-specific storage identifier type ? T <threads.h> C11

This header is available in C11.

Names matching ^thrd_[a-z] might be added to <threads.h>.

Many computing environments support concurrent execution within a single process, allowing them to take advantage of multiple processor cores. Each execution through a program is called a thread.

Multithreading is a natural way to exploit multiple processors, but it has complications. Threads of the same process share the same process state, including all static and dynamic objects, so it's easy for two threads to mistakenly manipulate a complex structure in such a way that it is left in an internally inconsistent state. Avoiding this problem requires concepts such as mutual exclusion and condition variables. Thread-local objects are safe, because such an object is actually a collection of objects, each one being accessible from only one thread. Automatic objects are safe, as long as they are not exposed by storing their addresses in static or dynamic structures.

C11 specifies a threading library in the form of a header <threads.h>. Not all implementations have to include it, so you can test for its availability with an idiom such as:

#if __STDC_VERSION__ > 201112 && __STDC_NO_THREADS__ != 1
#include <threads.h>
#else
#error "Threading unavailable"
#endif

<threads.h> defines thrd_t as a thread identifier. It also defines a number of return codes to indicate the outcome of attempting a thread-related operation. Many functions return thrd_success to indicate a successful operation. thrd_nomem is returned to indicate a lack of memory. thrd_error indicates a failed operation. thrd_busy indicates that a related operation would have blocked under the same conditions. thrd_timedout indicates that an operation timed out.

Function Return value
thrd_success thrd_error thrd_nomem thrd_busy thrd_timedout
thrd_create
thrd_detach
thrd_join
mtx_init
mtx_lock
mtx_trylock
mtx_timedlock
mtx_unlock
cnd_init
cnd_wait
cnd_timedwait
cnd_signal
cnd_broadcast
tss_create
tss_set
#include <threads.h>
typedef void (*thrd_start_t)(void *);
int thrd_create(thrd_t *thp, thrd_start_t func, void *arg);
int thrd_detach(thrd_t th);

thrd_create creates a new thread that will execute (*func)(arg). If successful, it assigns the new thread identifier to *thp, and returns thrd_success. If there is insufficient memory to start a new thread, it returns thrd_nomem. If it fails for any other reason, it returns thrd_error.

If the call to (*func)(arg) returns, it behaves as if the thread then calls thrd_exit(0).

thrd_detach tells the environment to deallocate resources for the thread identified by th when it terminates. It returns thrd_success on success, and thrd_error on failure.

#include <threads.h>
thrd_t thrd_current(void);
int thrd_equal(thrd_t a, thrd_t b);

thrd_current returns the identifier of the thread that called it. thrd_equal returns non-zero if the two arguments a and b identify the same thread.

#include <threads.h>
_Noreturn void thrd_exit(int st);
int thrd_join(thrd_t th, int *stp);

If a thread th calls thrd_exit, the thread terminates. If another thread calls thrd_join to join with the thread th, it will block until that thread terminates. The function then returns thrd_success. The argument st becomes the exit status of the thread, and is assigned to *stp for all threads waiting to join it. stp can be NULL, indicating that the calling thread does not require the exit status of th.

thrd_join returns thrd_error if the request to join a thread could not be honoured.

#include <threads.h>
void thrd_yield(void);
int thrd_sleep(const struct timespec *dur,
               struct timespec *rem);

thrd_yield gives other threads an opportunity to run even if the current thread is occupying execution resources, such as a physical processor. thrd_sleep makes the current thread wait for the duration specified by *dur. It then returns zero. If it is interrupted by a signal that is not being ignored with SIG_IGN, it will return prematurely with -1, having assigned the remaining time to *rem if rem is not null. If it fails, it returns some other negative value.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000