Namespaces
Variants
Views
Actions

Difference between revisions of "c/thread"

From cppreference.com
< c
(Atomic operations: N2653)
m ({{header}})
 
(8 intermediate revisions by 3 users not shown)
Line 6: Line 6:
 
These features are optionally provided:
 
These features are optionally provided:
 
* if the macro constant {{tt|__STDC_NO_THREADS__}} is defined by the compiler, the header {{tt|<threads.h>}} and all of the names provided in it are not provided;
 
* if the macro constant {{tt|__STDC_NO_THREADS__}} is defined by the compiler, the header {{tt|<threads.h>}} and all of the names provided in it are not provided;
* if the macro constant {{tt|__STDC_NO_ATOMICS__}} is defined by the compiler, the header {{tt|<stdatomic.h>}} and all of the names provided in it are not provided.
+
* if the macro constant {{tt|__STDC_NO_ATOMICS__}} is defined by the compiler, the header {{header|stdatomic.h}} and all of the names provided in it are not provided.
 +
 
 +
See also [[c/language/atomic|{{ttb|_Atomic}} type specifier and qualifier]].
  
 
===Threads===
 
===Threads===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | threads.h}}
+
{{dsc header|threads.h}}
{{dsc inc | c/thread/dsc thrd_t}}
+
{{dsc inc|c/thread/dsc thrd_t}}
{{dsc inc | c/thread/dsc thrd_create}}
+
{{dsc inc|c/thread/dsc thrd_create}}
{{dsc inc | c/thread/dsc thrd_equal}}
+
{{dsc inc|c/thread/dsc thrd_equal}}
{{dsc inc | c/thread/dsc thrd_current}}
+
{{dsc inc|c/thread/dsc thrd_current}}
{{dsc inc | c/thread/dsc thrd_sleep}}
+
{{dsc inc|c/thread/dsc thrd_sleep}}
{{dsc inc | c/thread/dsc thrd_yield}}
+
{{dsc inc|c/thread/dsc thrd_yield}}
{{dsc inc | c/thread/dsc thrd_exit}}
+
{{dsc inc|c/thread/dsc thrd_exit}}
{{dsc inc | c/thread/dsc thrd_detach}}
+
{{dsc inc|c/thread/dsc thrd_detach}}
{{dsc inc | c/thread/dsc thrd_join}}
+
{{dsc inc|c/thread/dsc thrd_join}}
{{dsc inc | c/thread/dsc thrd_errors}}
+
{{dsc inc|c/thread/dsc thrd_errors}}
{{dsc inc | c/thread/dsc thrd_start_t}}
+
{{dsc inc|c/thread/dsc thrd_start_t}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Atomic operations===
 
===Atomic operations===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | stdatomic.h}}
+
{{dsc header|stdatomic.h}}
{{dsc h2 | Operations on atomic types}}
+
{{dsc h2|Operations on atomic types}}
{{dsc inc | c/atomic/dsc ATOMIC_LOCK_FREE_consts}}
+
{{dsc inc|c/atomic/dsc ATOMIC_LOCK_FREE_consts}}
{{dsc inc | c/atomic/dsc atomic_is_lock_free}}
+
{{dsc inc|c/atomic/dsc atomic_is_lock_free}}
{{dsc inc | c/atomic/dsc atomic_store}}
+
{{dsc inc|c/atomic/dsc atomic_store}}
{{dsc inc | c/atomic/dsc atomic_load}}
+
{{dsc inc|c/atomic/dsc atomic_load}}
{{dsc inc | c/atomic/dsc atomic_exchange}}
+
{{dsc inc|c/atomic/dsc atomic_exchange}}
{{dsc inc | c/atomic/dsc atomic_compare_exchange}}
+
{{dsc inc|c/atomic/dsc atomic_compare_exchange}}
{{dsc inc | c/atomic/dsc atomic_fetch_add}}
+
{{dsc inc|c/atomic/dsc atomic_fetch_add}}
{{dsc inc | c/atomic/dsc atomic_fetch_sub}}
+
{{dsc inc|c/atomic/dsc atomic_fetch_sub}}
{{dsc inc | c/atomic/dsc atomic_fetch_or}}
+
{{dsc inc|c/atomic/dsc atomic_fetch_or}}
{{dsc inc | c/atomic/dsc atomic_fetch_xor}}
+
{{dsc inc|c/atomic/dsc atomic_fetch_xor}}
{{dsc inc | c/atomic/dsc atomic_fetch_and}}
+
{{dsc inc|c/atomic/dsc atomic_fetch_and}}
{{dsc h2 | Flag type and operations}}
+
{{dsc h2|Flag type and operations}}
{{dsc inc | c/atomic/dsc atomic_flag}}
+
{{dsc inc|c/atomic/dsc atomic_flag}}
{{dsc inc | c/atomic/dsc atomic_flag_test_and_set}}
+
{{dsc inc|c/atomic/dsc atomic_flag_test_and_set}}
{{dsc inc | c/atomic/dsc atomic_flag_clear}}
+
{{dsc inc|c/atomic/dsc atomic_flag_clear}}
{{dsc h2 | Initialization}}
+
{{dsc h2|Initialization}}
{{dsc inc | c/atomic/dsc atomic_init}}
+
{{dsc inc|c/atomic/dsc atomic_init}}
{{dsc inc | c/atomic/dsc ATOMIC_VAR_INIT}}
+
{{dsc inc|c/atomic/dsc ATOMIC_VAR_INIT}}
{{dsc inc | c/atomic/dsc ATOMIC_FLAG_INIT}}
+
{{dsc inc|c/atomic/dsc ATOMIC_FLAG_INIT}}
{{dsc h2 | Memory synchronization ordering}}
+
{{dsc h2|Memory synchronization ordering}}
{{dsc inc | c/atomic/dsc memory_order}}
+
{{dsc inc|c/atomic/dsc memory_order}}
{{dsc inc | c/atomic/dsc kill_dependency}}
+
{{dsc inc|c/atomic/dsc kill_dependency}}
{{dsc inc | c/atomic/dsc atomic_thread_fence}}
+
{{dsc inc|c/atomic/dsc atomic_thread_fence}}
{{dsc inc | c/atomic/dsc atomic_signal_fence}}
+
{{dsc inc|c/atomic/dsc atomic_signal_fence}}
{{dsc h2 | Convenience type aliases}}
+
{{dsc h2|Convenience type aliases}}
{{dsc hitem | Typedef name | Full type name }}
+
{{dsc hitem|Typedef name|Full type name}}
{{dsc | {{tt|atomic_bool}}{{mark c11}} | {{c|_Atomic _Bool}} }}
+
{{dsc|{{tt|atomic_bool}}{{mark c11}}|{{c|_Atomic _Bool}}}}
{{dsc | {{tt|atomic_char}}{{mark c11}} | {{c|_Atomic char}} }}
+
{{dsc|{{tt|atomic_char}}{{mark c11}}|{{c|_Atomic char}}}}
{{dsc | {{tt|atomic_schar}}{{mark c11}} | {{c|_Atomic signed char}} }}
+
{{dsc|{{tt|atomic_schar}}{{mark c11}}|{{c|_Atomic signed char}}}}
{{dsc | {{tt|atomic_uchar}}{{mark c11}} | {{c|_Atomic unsigned char}} }}
+
{{dsc|{{tt|atomic_uchar}}{{mark c11}}|{{c|_Atomic unsigned char}}}}
{{dsc | {{tt|atomic_short}}{{mark c11}} | {{c|_Atomic short}} }}
+
{{dsc|{{tt|atomic_short}}{{mark c11}}|{{c|_Atomic short}}}}
{{dsc | {{tt|atomic_ushort}}{{mark c11}} | {{c|_Atomic unsigned short}} }}
+
{{dsc|{{tt|atomic_ushort}}{{mark c11}}|{{c|_Atomic unsigned short}}}}
{{dsc | {{tt|atomic_int}}{{mark c11}} | {{c|_Atomic int}} }}
+
{{dsc|{{tt|atomic_int}}{{mark c11}}|{{c|_Atomic int}}}}
{{dsc | {{tt|atomic_uint}}{{mark c11}} | {{c|_Atomic unsigned int}} }}
+
{{dsc|{{tt|atomic_uint}}{{mark c11}}|{{c|_Atomic unsigned int}}}}
{{dsc | {{tt|atomic_long}}{{mark c11}} | {{c|_Atomic long}} }}
+
{{dsc|{{tt|atomic_long}}{{mark c11}}|{{c|_Atomic long}}}}
{{dsc | {{tt|atomic_ulong}}{{mark c11}} | {{c|_Atomic unsigned long}} }}
+
{{dsc|{{tt|atomic_ulong}}{{mark c11}}|{{c|_Atomic unsigned long}}}}
{{dsc | {{tt|atomic_llong}}{{mark c11}} | {{c|_Atomic long long}} }}
+
{{dsc|{{tt|atomic_llong}}{{mark c11}}|{{c|_Atomic long long}}}}
{{dsc | {{tt|atomic_ullong}}{{mark c11}} | {{c|_Atomic unsigned long long}} }}
+
{{dsc|{{tt|atomic_ullong}}{{mark c11}}|{{c|_Atomic unsigned long long}}}}
{{dsc | {{tt|atomic_char8_t}}{{mark c23}} | {{c|_Atomic char8_t}} }}
+
{{dsc|{{tt|atomic_char8_t}}{{mark c23}}|{{c|_Atomic char8_t}}}}
{{dsc | {{tt|atomic_char16_t}}{{mark c11}} | {{c|_Atomic char16_t}} }}
+
{{dsc|{{tt|atomic_char16_t}}{{mark c11}}|{{c|_Atomic char16_t}}}}
{{dsc | {{tt|atomic_char32_t}}{{mark c11}} | {{c|_Atomic char32_t}} }}
+
{{dsc|{{tt|atomic_char32_t}}{{mark c11}}|{{c|_Atomic char32_t}}}}
{{dsc | {{tt|atomic_wchar_t}}{{mark c11}} | {{c|_Atomic wchar_t}} }}
+
{{dsc|{{tt|atomic_wchar_t}}{{mark c11}}|{{c|_Atomic wchar_t}}}}
{{dsc | {{tt|atomic_int_least8_t}}{{mark c11}} | {{c|_Atomic int_least8_t}} }}
+
{{dsc|{{tt|atomic_int_least8_t}}{{mark c11}}|{{c|_Atomic int_least8_t}}}}
{{dsc | {{tt|atomic_uint_least8_t}}{{mark c11}} | {{c|_Atomic uint_least8_t}} }}
+
{{dsc|{{tt|atomic_uint_least8_t}}{{mark c11}}|{{c|_Atomic uint_least8_t}}}}
{{dsc | {{tt|atomic_int_least16_t}}{{mark c11}} | {{c|_Atomic int_least16_t}} }}
+
{{dsc|{{tt|atomic_int_least16_t}}{{mark c11}}|{{c|_Atomic int_least16_t}}}}
{{dsc | {{tt|atomic_uint_least16_t}}{{mark c11}} | {{c|_Atomic uint_least16_t}} }}
+
{{dsc|{{tt|atomic_uint_least16_t}}{{mark c11}}|{{c|_Atomic uint_least16_t}}}}
{{dsc | {{tt|atomic_int_least32_t}}{{mark c11}} | {{c|_Atomic int_least32_t}} }}
+
{{dsc|{{tt|atomic_int_least32_t}}{{mark c11}}|{{c|_Atomic int_least32_t}}}}
{{dsc | {{tt|atomic_uint_least32_t}}{{mark c11}} | {{c|_Atomic uint_least32_t}} }}
+
{{dsc|{{tt|atomic_uint_least32_t}}{{mark c11}}|{{c|_Atomic uint_least32_t}}}}
{{dsc | {{tt|atomic_int_least64_t}}{{mark c11}} | {{c|_Atomic int_least64_t}} }}
+
{{dsc|{{tt|atomic_int_least64_t}}{{mark c11}}|{{c|_Atomic int_least64_t}}}}
{{dsc | {{tt|atomic_uint_least64_t}}{{mark c11}} | {{c|_Atomic uint_least64_t}} }}
+
{{dsc|{{tt|atomic_uint_least64_t}}{{mark c11}}|{{c|_Atomic uint_least64_t}}}}
{{dsc | {{tt|atomic_int_fast8_t}}{{mark c11}} | {{c|_Atomic int_fast8_t}} }}
+
{{dsc|{{tt|atomic_int_fast8_t}}{{mark c11}}|{{c|_Atomic int_fast8_t}}}}
{{dsc | {{tt|atomic_uint_fast8_t}}{{mark c11}} | {{c|_Atomic uint_fast8_t}} }}
+
{{dsc|{{tt|atomic_uint_fast8_t}}{{mark c11}}|{{c|_Atomic uint_fast8_t}}}}
{{dsc | {{tt|atomic_int_fast16_t}}{{mark c11}} | {{c|_Atomic int_fast16_t}} }}
+
{{dsc|{{tt|atomic_int_fast16_t}}{{mark c11}}|{{c|_Atomic int_fast16_t}}}}
{{dsc | {{tt|atomic_uint_fast16_t}}{{mark c11}} | {{c|_Atomic uint_fast16_t}} }}
+
{{dsc|{{tt|atomic_uint_fast16_t}}{{mark c11}}|{{c|_Atomic uint_fast16_t}}}}
{{dsc | {{tt|atomic_int_fast32_t}}{{mark c11}} | {{c|_Atomic int_fast32_t}} }}
+
{{dsc|{{tt|atomic_int_fast32_t}}{{mark c11}}|{{c|_Atomic int_fast32_t}}}}
{{dsc | {{tt|atomic_uint_fast32_t}}{{mark c11}} | {{c|_Atomic uint_fast32_t}} }}
+
{{dsc|{{tt|atomic_uint_fast32_t}}{{mark c11}}|{{c|_Atomic uint_fast32_t}}}}
{{dsc | {{tt|atomic_int_fast64_t}}{{mark c11}} | {{c|_Atomic int_fast64_t}} }}
+
{{dsc|{{tt|atomic_int_fast64_t}}{{mark c11}}|{{c|_Atomic int_fast64_t}}}}
{{dsc | {{tt|atomic_uint_fast64_t}}{{mark c11}} | {{c|_Atomic uint_fast64_t}} }}
+
{{dsc|{{tt|atomic_uint_fast64_t}}{{mark c11}}|{{c|_Atomic uint_fast64_t}}}}
{{dsc | {{tt|atomic_intptr_t}}{{mark c11}} | {{c|_Atomic intptr_t}} }}
+
{{dsc|{{tt|atomic_intptr_t}}{{mark c11}}|{{c|_Atomic intptr_t}}}}
{{dsc | {{tt|atomic_uintptr_t}}{{mark c11}} | {{c|_Atomic uintptr_t}} }}
+
{{dsc|{{tt|atomic_uintptr_t}}{{mark c11}}|{{c|_Atomic uintptr_t}}}}
{{dsc | {{tt|atomic_size_t}}{{mark c11}} | {{c|_Atomic size_t}} }}
+
{{dsc|{{tt|atomic_size_t}}{{mark c11}}|{{c|_Atomic size_t}}}}
{{dsc | {{tt|atomic_ptrdiff_t}}{{mark c11}} | {{c|_Atomic ptrdiff_t}} }}
+
{{dsc|{{tt|atomic_ptrdiff_t}}{{mark c11}}|{{c|_Atomic ptrdiff_t}}}}
{{dsc | {{tt|atomic_intmax_t}}{{mark c11}} | {{c|_Atomic intmax_t}} }}
+
{{dsc|{{tt|atomic_intmax_t}}{{mark c11}}|{{c|_Atomic intmax_t}}}}
{{dsc | {{tt|atomic_uintmax_t}}{{mark c11}} | {{c|_Atomic uintmax_t}} }}
+
{{dsc|{{tt|atomic_uintmax_t}}{{mark c11}}|{{c|_Atomic uintmax_t}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Mutual exclusion===
 
===Mutual exclusion===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | threads.h}}
+
{{dsc header|threads.h}}
{{dsc inc | c/thread/dsc mtx_t}}
+
{{dsc inc|c/thread/dsc mtx_t}}
{{dsc inc | c/thread/dsc mtx_init}}
+
{{dsc inc|c/thread/dsc mtx_init}}
{{dsc inc | c/thread/dsc mtx_lock}}
+
{{dsc inc|c/thread/dsc mtx_lock}}
{{dsc inc | c/thread/dsc mtx_timedlock}}
+
{{dsc inc|c/thread/dsc mtx_timedlock}}
{{dsc inc | c/thread/dsc mtx_trylock}}
+
{{dsc inc|c/thread/dsc mtx_trylock}}
{{dsc inc | c/thread/dsc mtx_unlock}}
+
{{dsc inc|c/thread/dsc mtx_unlock}}
{{dsc inc | c/thread/dsc mtx_destroy}}
+
{{dsc inc|c/thread/dsc mtx_destroy}}
{{dsc inc | c/thread/dsc mtx_types}}
+
{{dsc inc|c/thread/dsc mtx_types}}
{{dsc h2 | Call once}}
+
{{dsc h2|Call once}}
{{dsc inc | c/thread/dsc call_once}}
+
{{dsc inc|c/thread/dsc call_once}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Condition variables===
 
===Condition variables===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | threads.h}}
+
{{dsc header|threads.h}}
{{dsc | {{ttb|cnd_t}} | condition variable identifier | notes={{mark c11}}}}
+
{{dsc|{{ttb|cnd_t}}|condition variable identifier|notes={{mark c11}}}}
{{dsc inc | c/thread/dsc cnd_init}}
+
{{dsc inc|c/thread/dsc cnd_init}}
{{dsc inc | c/thread/dsc cnd_signal}}
+
{{dsc inc|c/thread/dsc cnd_signal}}
{{dsc inc | c/thread/dsc cnd_broadcast}}
+
{{dsc inc|c/thread/dsc cnd_broadcast}}
{{dsc inc | c/thread/dsc cnd_wait}}
+
{{dsc inc|c/thread/dsc cnd_wait}}
{{dsc inc | c/thread/dsc cnd_timedwait}}
+
{{dsc inc|c/thread/dsc cnd_timedwait}}
{{dsc inc | c/thread/dsc cnd_destroy}}
+
{{dsc inc|c/thread/dsc cnd_destroy}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Thread-local storage===
 
===Thread-local storage===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | threads.h}}
+
{{dsc header|threads.h}}
{{dsc inc | c/thread/dsc thread_local}}
+
{{dsc inc|c/thread/dsc thread_local}}
{{dsc inc | c/thread/dsc tss_t}}
+
{{dsc inc|c/thread/dsc tss_t}}
{{dsc inc | c/thread/dsc TSS_DTOR_ITERATIONS}}
+
{{dsc inc|c/thread/dsc TSS_DTOR_ITERATIONS}}
{{dsc inc | c/thread/dsc tss_dtor_t}}
+
{{dsc inc|c/thread/dsc tss_dtor_t}}
{{dsc inc | c/thread/dsc tss_create}}
+
{{dsc inc|c/thread/dsc tss_create}}
{{dsc inc | c/thread/dsc tss_get}}
+
{{dsc inc|c/thread/dsc tss_get}}
{{dsc inc | c/thread/dsc tss_set}}
+
{{dsc inc|c/thread/dsc tss_set}}
{{dsc inc | c/thread/dsc tss_delete}}
+
{{dsc inc|c/thread/dsc tss_delete}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 137: Line 139:
 
In future revisions of the C standard:
 
In future revisions of the C standard:
 
* function names, type names, and enumeration constants that begin with either {{tt|cnd_}}, {{tt|mtx_}}, {{tt|thrd_}}, or {{tt|tss_}}, and a lowercase letter may be added to the declarations in the {{tt|<threads.h>}} header;
 
* function names, type names, and enumeration constants that begin with either {{tt|cnd_}}, {{tt|mtx_}}, {{tt|thrd_}}, or {{tt|tss_}}, and a lowercase letter may be added to the declarations in the {{tt|<threads.h>}} header;
* macros that begin with {{tt|ATOMIC_}} and an uppercase letter may be added to the macros defined in the {{tt|<stdatomic.h>}} header;
+
* macros that begin with {{tt|ATOMIC_}} and an uppercase letter may be added to the macros defined in the {{header|stdatomic.h}} header;
* typedef names that begin with either {{tt|atomic_}} or {{tt|memory_}}, and a lowercase letter may be added to the declarations in the {{tt|<stdatomic.h>}} header;
+
* typedef names that begin with either {{tt|atomic_}} or {{tt|memory_}}, and a lowercase letter may be added to the declarations in the {{header|stdatomic.h}} header;
* enumeration constants that begin with {{tt|memory_order_}} and a lowercase letter may be added to the definition of the {{lc|memory_order}} type in the {{tt|<stdatomic.h>}} header;
+
* enumeration constants that begin with {{tt|memory_order_}} and a lowercase letter may be added to the definition of the {{lc|memory_order}} type in the {{header|stdatomic.h}} header;
* function names that begin with {{tt|atomic_}} and a lowercase letter may be added to the declarations in the {{tt|<stdatomic.h>}} header.
+
* function names that begin with {{tt|atomic_}} and a lowercase letter may be added to the declarations in the {{header|stdatomic.h}} header.
  
Identifiers reserved for functions names are always {{rev inl|since=c23|potentially}} reserved for use as identifiers with external linkage, while other identifiers list here are {{rev inl|since=c23|potentially}} reserved when {{tt|<stdatomic.h>}} is included.
+
Identifiers reserved for functions names are always {{rev inl|since=c23|potentially}} reserved for use as identifiers with external linkage, while other identifiers list here are {{rev inl|since=c23|potentially}} reserved when {{header|stdatomic.h}} is included.
  
 
Declaring, defining, or {{c|#undef}}ing such an identifier results in undefined behavior{{rev inl|since=c23| if it is provided by the standard or implementation}}. Portable programs should not use those identifiers.
 
Declaring, defining, or {{c|#undef}}ing such an identifier results in undefined behavior{{rev inl|since=c23| if it is provided by the standard or implementation}}. Portable programs should not use those identifiers.
  
 
===References===
 
===References===
 +
{{ref std c23}}
 +
{{ref std|section=7.17|title=Atomics <stdatomic.h>|p=TBD}}
 +
{{ref std|section=7.26|title=Threads <threads.h>|p=TBD}}
 +
{{ref std|section=7.31.8|title=Atomics <stdatomic.h>|p=TBD}}
 +
{{ref std|section=7.31.15|title=Threads <threads.h>|p=TBD}}
 +
{{ref std end}}
 
{{ref std c17}}
 
{{ref std c17}}
{{ref std | section=7.17 | title=Atomics <stdatomic.h> | p=200-209}}
+
{{ref std|section=7.17|title=Atomics <stdatomic.h>|p=200-209}}
{{ref std | section=7.26 | title=Threads <threads.h> | p=274-283}}
+
{{ref std|section=7.26|title=Threads <threads.h>|p=274-283}}
{{ref std | section=7.31.8 | title=Atomics <stdatomic.h> | p=332}}
+
{{ref std|section=7.31.8|title=Atomics <stdatomic.h>|p=332}}
{{ref std | section=7.31.15 | title=Threads <threads.h> | p=333}}
+
{{ref std|section=7.31.15|title=Threads <threads.h>|p=333}}
 
{{ref std end}}
 
{{ref std end}}
 
{{ref std c11}}
 
{{ref std c11}}
{{ref std | section=7.17 | title=Atomics <stdatomic.h> | p=273-286}}
+
{{ref std|section=7.17|title=Atomics <stdatomic.h>|p=273-286}}
{{ref std | section=7.26 | title=Threads <threads.h> | p=376-387}}
+
{{ref std|section=7.26|title=Threads <threads.h>|p=376-387}}
{{ref std | section=7.31.8 | title=Atomics <stdatomic.h> | p=455-456}}
+
{{ref std|section=7.31.8|title=Atomics <stdatomic.h>|p=455-456}}
{{ref std | section=7.31.15 | title=Threads <threads.h> | p=456}}
+
{{ref std|section=7.31.15|title=Threads <threads.h>|p=456}}
 
{{ref std end}}
 
{{ref std end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see cpp | cpp/thread | Concurrency support library | nomono=true}}
+
{{dsc see cpp|cpp/thread|Concurrency support library|nomono=true}}
{{dsc | [https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html GNU GCC Libc Manual: ISO-C-Mutexes]}}
+
 
{{dsc end}}
 
{{dsc end}}
 +
 +
===External links===
 +
{{eli|[https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html GNU GCC Libc Manual: ISO C Mutexes]}}
  
 
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}
 
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}

Latest revision as of 02:14, 14 August 2023

C includes built-in support for threads, atomic operations, mutual exclusion, condition variables, and thread-specific storages.

These features are optionally provided:

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

Contents

[edit] Threads

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

[edit] Atomic operations

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

[edit] Mutual exclusion

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

[edit] Condition variables

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

[edit] Thread-local storage

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

[edit] Reserved identifiers

In future revisions of the C standard:

  • 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 C23) reserved for use as identifiers with external linkage, while other identifiers list here are potentially(since C23) 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 C23). Portable programs should not use those identifiers.

[edit] References

  • C23 standard (ISO/IEC 9899:2024):
  • 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)
  • C17 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)
  • C11 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)

[edit] See also

C++ documentation for Concurrency support library

[edit] External links

GNU GCC Libc Manual: ISO C Mutexes