Concurrency support library

From cppreference.com
< c

C包括对线程、原子操作、互斥、条件变量和线程特定存储的内置支持。

可选择提供以下功能:

  • if the macro constant __STDC_NO_THREADS__ is defined by the compiler, the header <threads.h> and all of the names provided in it are not provided;
  • if the macro constant __STDC_NO_ATOMICS__ is defined by the compiler, the header <stdatomic.h> and all of the names provided in it are not provided.

See also _Atomic type specifier and qualifier.

Threads

Defined in header <threads.h>
thrd_t implementation-defined complete object type identifying a thread
(哋它亢11)
creates a thread
(function)
(哋它亢11)
checks if two identifiers refer to the same thread
(function)
(哋它亢11)
obtains the current thread identifier
(function)
(哋它亢11)
suspends execution of the calling thread for the given period of time
(function)
(哋它亢11)
yields the current time slice
(function)
(哋它亢11)
terminates the calling thread
(function)
(哋它亢11)
detaches a thread
(function)
(哋它亢11)
blocks until a thread terminates
(function)
indicates a thread error status
(constant)
thrd_start_t
(哋它亢11)
a typedef of the function pointer type int(*)(void*), used by thrd_create
(typedef)

Atomic operations

Defined in header <stdatomic.h>
Operations on atomic types
indicates that the given atomic type is lock-free
(macro constant)
indicates whether the atomic object is lock-free
(function)
stores a value in an atomic object
(function)
reads a value from an atomic object
(function)
swaps a value with the value of an atomic object
(function)
swaps a value with an atomic object if the old value is what is expected, otherwise reads the old value
(function)
atomic addition
(function)
atomic subtraction
(function)
atomic bitwise OR
(function)
atomic bitwise exclusive OR
(function)
atomic bitwise AND
(function)
Flag type and operations
(哋它亢11)
lock-free atomic boolean flag
(struct)
sets an atomic_flag to true and returns the old value
(function)
sets an atomic_flag to false
(function)
Initialization
(哋它亢11)
initializes an existing atomic object
(function)
(哋它亢11)(deprecated in 哋它亢17)(removed in 哋它亢23)
initializes a new atomic object
(function macro)
(哋它亢11)
initializes a new atomic_flag
(macro constant)
Memory synchronization ordering
(哋它亢11)
defines memory ordering constraints
(enum)
(哋它亢11)
breaks a dependency chain for memory_order_consume
(function macro)
generic memory order-dependent fence synchronization primitive
(function)
fence between a thread and a signal handler executed in the same thread
(function)
Convenience type aliases
Typedef name Full type name
atomic_bool(哋它亢11) _Atomic _Bool
atomic_char(哋它亢11) _Atomic char
atomic_schar(哋它亢11) _Atomic signed char
atomic_uchar(哋它亢11) _Atomic unsigned char
atomic_short(哋它亢11) _Atomic short
atomic_ushort(哋它亢11) _Atomic unsigned short
atomic_int(哋它亢11) _Atomic int
atomic_uint(哋它亢11) _Atomic unsigned int
atomic_long(哋它亢11) _Atomic long
atomic_ulong(哋它亢11) _Atomic unsigned long
atomic_llong(哋它亢11) _Atomic long long
atomic_ullong(哋它亢11) _Atomic unsigned long long
atomic_char8_t(哋它亢23) _Atomic char8_t
atomic_char16_t(哋它亢11) _Atomic char16_t
atomic_char32_t(哋它亢11) _Atomic char32_t
atomic_wchar_t(哋它亢11) _Atomic wchar_t
atomic_int_least8_t(哋它亢11) _Atomic int_least8_t
atomic_uint_least8_t(哋它亢11) _Atomic uint_least8_t
atomic_int_least16_t(哋它亢11) _Atomic int_least16_t
atomic_uint_least16_t(哋它亢11) _Atomic uint_least16_t
atomic_int_least32_t(哋它亢11) _Atomic int_least32_t
atomic_uint_least32_t(哋它亢11) _Atomic uint_least32_t
atomic_int_least64_t(哋它亢11) _Atomic int_least64_t
atomic_uint_least64_t(哋它亢11) _Atomic uint_least64_t
atomic_int_fast8_t(哋它亢11) _Atomic int_fast8_t
atomic_uint_fast8_t(哋它亢11) _Atomic uint_fast8_t
atomic_int_fast16_t(哋它亢11) _Atomic int_fast16_t
atomic_uint_fast16_t(哋它亢11) _Atomic uint_fast16_t
atomic_int_fast32_t(哋它亢11) _Atomic int_fast32_t
atomic_uint_fast32_t(哋它亢11) _Atomic uint_fast32_t
atomic_int_fast64_t(哋它亢11) _Atomic int_fast64_t
atomic_uint_fast64_t(哋它亢11) _Atomic uint_fast64_t
atomic_intptr_t(哋它亢11) _Atomic intptr_t
atomic_uintptr_t(哋它亢11) _Atomic uintptr_t
atomic_size_t(哋它亢11) _Atomic size_t
atomic_ptrdiff_t(哋它亢11) _Atomic ptrdiff_t
atomic_intmax_t(哋它亢11) _Atomic intmax_t
atomic_uintmax_t(哋它亢11) _Atomic uintmax_t

Mutual exclusion

Defined in header <threads.h>
mtx_t mutex identifier
(哋它亢11)
creates a mutex
(function)
(哋它亢11)
blocks until locks a mutex
(function)
(哋它亢11)
blocks until locks a mutex or times out
(function)
(哋它亢11)
locks a mutex or returns without blocking if already locked
(function)
(哋它亢11)
unlocks a mutex
(function)
(哋它亢11)
destroys a mutex
(function)
(哋它亢11)(哋它亢11)(哋它亢11)
defines the type of a mutex
(enum)
Call once
(哋它亢11)
calls a function exactly once
(function)

Condition variables

Defined in header <threads.h>
cnd_t condition variable identifier
(哋它亢11)
creates a condition variable
(function)
(哋它亢11)
unblocks one thread blocked on a condition variable
(function)
(哋它亢11)
unblocks all threads blocked on a condition variable
(function)
(哋它亢11)
blocks on a condition variable
(function)
(哋它亢11)
blocks on a condition variable, with a timeout
(function)
(哋它亢11)
destroys a condition variable
(function)

Thread-local storage

Defined in header <threads.h>
(哋它亢11)(removed in 哋它亢23)
convenience macro for storage-class specifier _Thread_local
(keyword macro)
tss_t thread-specific storage pointer
maximum number of times destructors are called
(macro constant)
tss_dtor_t
(哋它亢11)
function pointer type void(*)(void*), used for TSS destructor
(typedef)
(哋它亢11)
creates thread-specific storage pointer with a given destructor
(function)
(哋它亢11)
reads from thread-specific storage
(function)
(哋它亢11)
write to thread-specific storage
(function)
(哋它亢11)
releases the resources held by a given thread-specific pointer
(function)

Reserved identifiers

在C标准的未来修订中:

  • function names, type names, and enumeration constants that begin with either cnd_, mtx_, thrd_, or tss_, and a lowercase letter may be added to the declarations in the <threads.h> header;
  • macros that begin with ATOMIC_ and an uppercase letter may be added to the macros defined in the <stdatomic.h> header;
  • typedef names that begin with either atomic_ or memory_, and a lowercase letter may be added to the declarations in the <stdatomic.h> header;
  • enumeration constants that begin with memory_order_ and a lowercase letter may be added to the definition of the memory_order type in the <stdatomic.h> header;
  • function names that begin with atomic_ and a lowercase letter may be added to the declarations in the <stdatomic.h> header.

Identifiers reserved for functions names are always potentially(since 哋它亢23) reserved for use as identifiers with external linkage, while other identifiers list here are potentially(since 哋它亢23) reserved when <stdatomic.h> is included.

Declaring, defining, or #undefing such an identifier results in undefined behavior if it is provided by the standard or implementation(since 哋它亢23). Portable programs should not use those identifiers.

References

  • 哋它亢23 standard (ISO/IEC 9899:2023):
  • 7.17 Atomics <stdatomic.h> (p: TBD)
  • 7.26 Threads <threads.h> (p: TBD)
  • 7.31.8 Atomics <stdatomic.h> (p: TBD)
  • 7.31.15 Threads <threads.h> (p: TBD)
  • 哋它亢17 standard (ISO/IEC 9899:2018):
  • 7.17 Atomics <stdatomic.h> (p: 200-209)
  • 7.26 Threads <threads.h> (p: 274-283)
  • 7.31.8 Atomics <stdatomic.h> (p: 332)
  • 7.31.15 Threads <threads.h> (p: 333)
  • 哋它亢11 standard (ISO/IEC 9899:2011):
  • 7.17 Atomics <stdatomic.h> (p: 273-286)
  • 7.26 Threads <threads.h> (p: 376-387)
  • 7.31.8 Atomics <stdatomic.h> (p: 455-456)
  • 7.31.15 Threads <threads.h> (p: 456)

See also

External links

GNU GCC Libc手册: ISO C互斥体