threads.h File

Purpose

The threads.h header file includes the time.h header file and defines macros It also declares types, enumeration constants, and functions that support multiple threads of execution.

Description

The header threads.h header file must not be provided by implementations that define the macro _ _STDC_NO_THREADS_ _ . Also, such implementations must not support any facilities that are specified by the header threads.h file.

The threads.h header file declares the following macros:
thread_local
Expands to _Thread_local.
ONCE_FLAG_INIT
Expands to a value that can be used to initialize an object of the type once_flag.
TSS_DTOR_ITERATIONS
Expands to an integer constant expression that represents the maximum number of times a destructor is called when a thread terminates.
The threads.h header file declares following types:
cnd_t
A complete object type that holds an identifier for a condition variable.
thrd_t
A complete object type that holds an identifier for a thread.
tss_t
A complete object type that holds an identifier for a thread-specific storage pointer.
mtx_t
A complete object type that holds an identifier for a mutex.
tss_dtor_t
A function pointer type void (*)(void*), used for a destructor for a thread-specific storage pointer.
thrd_start_t
A function pointer type int (*)(void*) that is passed to thrd_create to create a new thread.
once_flag
A complete object type that holds a flag for use by call_once.
The threads.h header file declares the following enumeration constants:
mtx_plain
The mtx_plain type is passed to mtx_init to create a mutex object that does not support timeout and test and returns.
mtx_recursive
The mtx_recursive type is passed to mtx_init to create a mutex object that supports recursive locking.
mtx_timed
The mtx_timed type is passed to mtx_init to create a mutex object that supports timeout.
thrd_timedout
The cnd_t type is returned by a timed wait function to indicate that the time specified in the call was reached without acquiring the requested resource.
thrd_success
The thrd_success type is returned by the function to indicate that the requested operation succeeded.
thrd_busy
The thrd_busy type is returned by the function to indicate that the requested operation failed because a resource requested by a test and return functions is already in use.
thrd_error
The thrd_error type is returned by a function to indicate that the requested operation failed.
thrd_nomem
The thrd_nomem type is returned by a function to indicate that the requested operation failed because it was unable to allocate memory.
The threads.h header file declares the following functions:
void call_once(once_flag *flag, void (*func)(void));
int cnd_broadcast(cnd_t *cond);
void cnd_destroy(cnd_t *cond);
int cnd_init(cnd_t *cond);
int cnd_signal(cnd_t *cond);
int cnd_timedwait(cnd_t *restrict cond,
mtx_t *restrict mtx,
const struct timespec *restrict ts);
int cnd_wait(cnd_t *cond, mtx_t *mtx);
void mtx_destroy(mtx_t *mtx);
int mtx_init(mtx_t *mtx, int type);
int mtx_lock(mtx_t *mtx);
int mtx_timedlock(mtx_t *restrict mtx,
const struct timespec *restrict ts);
int mtx_trylock(mtx_t *mtx);
int mtx_unlock(mtx_t *mtx);
int thrd_create(thrd_t *thr, thrd_start_t func,
void *arg);
thrd_t thrd_current(void);
int thrd_detach(thrd_t thr);
int thrd_equal(thrd_t thr0, thrd_t thr1);
_Noreturn void thrd_exit(int res);
int thrd_join(thrd_t thr, int *res);
int thrd_sleep(const struct timespec *duration,
struct timespec *remaining);
void thrd_yield(void);
int tss_create(tss_t *key, tss_dtor_t dtor);
void tss_delete(tss_t key);
void *tss_get(tss_t key);
int tss_set(tss_t key, void *val);