Difference between revisions of "cpp/thread"
(rm redundant info) |
(→cooperative cancellation: update description) |
||
(78 intermediate revisions by 21 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title| | + | {{title|Concurrency support library {{mark since c++11}}}} |
− | {{cpp/thread/ | + | {{cpp/thread/navbar}} |
+ | |||
+ | C++ includes built-in support for threads, atomic operations, mutual exclusion, condition variables, and futures. | ||
+ | |||
===Threads=== | ===Threads=== | ||
+ | Threads enable programs to execute across several processor cores. | ||
− | + | {{dsc begin}} | |
+ | {{dsc header|thread}} | ||
+ | {{dsc inc|cpp/thread/dsc thread}} | ||
+ | {{dsc inc|cpp/thread/dsc jthread}} | ||
+ | {{dsc h2|Functions managing the current thread}} | ||
+ | {{dsc namespace|this_thread}} | ||
+ | {{dsc inc|cpp/thread/dsc yield}} | ||
+ | {{dsc inc|cpp/thread/dsc get_id}} | ||
+ | {{dsc inc|cpp/thread/dsc sleep_for}} | ||
+ | {{dsc inc|cpp/thread/dsc sleep_until}} | ||
+ | {{dsc end}} | ||
− | {{ | + | {{rrev|since=c++20| |
− | {{ | + | ===Cooperative cancellation=== |
− | {{ | + | The components ''stop source'', ''stop token'', and ''stop callback'' can be used to asynchronously request that an operation stops execution in a timely manner, typically because the result is no longer required. Such a request is called a ''stop request''. |
− | {{ | + | |
− | {{ | + | These components specify the semantics of shared access to a ''stop state''. Any object modeling any of these components that refer to the same stop state is an associated stop source, stop token, or stop callback, respectively. |
− | {{ | + | |
− | {{ | + | {{rrev|since=c++26| |
− | {{ | + | The concepts {{lconcept|stoppable-source}}, {{lconcept|stoppable_token}}, and {{lconcept|stoppable-callback-for}} specify the required syntax and model semantics of stop source, stop token, and stop callback, respectively. |
− | {{ | + | }} |
− | {{ | + | |
+ | They are designed: | ||
+ | * to cooperatively cancel the execution such as for {{lc|std::jthread}}, | ||
+ | * to interrupt {{lc|std::condition_variable_any}} waiting functions, | ||
+ | {{rrev|since=c++26|* to perform stopped completion of an asynchronous operation created by {{lc|execution::connect}},}} | ||
+ | * or for a custom execution management implementation. | ||
+ | In fact, they do not even need to be used to "stop" anything, but can instead be used for a thread-safe one-time function(s) invocation trigger, for example. | ||
+ | |||
+ | {{dsc begin}} | ||
+ | {{dsc header|stop_token}} | ||
+ | {{dsc h2|Stop token types}} | ||
+ | {{dsc inc|cpp/thread/dsc stop_token}} | ||
+ | {{dsc inc|cpp/thread/dsc never_stop_token}} | ||
+ | {{dsc inc|cpp/thread/dsc inplace_stop_token}} | ||
+ | {{dsc h2|Stop source types}} | ||
+ | {{dsc inc|cpp/thread/dsc stop_source}}<!--include nostopstate_t inside as helper--> | ||
+ | {{dsc inc|cpp/thread/dsc inplace_stop_source}} | ||
+ | {{dsc h2|Stop callback types}} | ||
+ | {{dsc inc|cpp/thread/dsc stop_callback}} | ||
+ | {{dsc inc|cpp/thread/dsc inplace_stop_callback}} | ||
+ | {{dsc inc|cpp/thread/dsc stop_callback_for_t}} | ||
+ | {{dsc h2|Concepts}} | ||
+ | {{dsc inc|cpp/thread/dsc stoppable_token}} | ||
+ | {{dsc inc|cpp/thread/dsc unstoppable_token}} | ||
+ | {{dsc inc|cpp/thread/dsc stoppable_source}} | ||
+ | {{dsc inc|cpp/thread/dsc stoppable_callback_for}} | ||
+ | {{dsc end}} | ||
+ | }} | ||
+ | |||
+ | ===Cache size access=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc header|new}} | ||
+ | {{dsc inc|cpp/thread/dsc hardware_destructive_interference_size}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | ===Atomic operations=== | ||
+ | These components are provided for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object. Atomic objects are [[cpp/language/memory_model#Threads_and_data_races|free of data races]]. | ||
+ | |||
+ | {{rrev|since=c++23| | ||
+ | Neither the {{tt|_Atomic}} macro, nor any of the non-macro global namespace declarations are provided by any C++ standard library header other than {{tt|<stdatomic.h>}}. | ||
+ | }} | ||
+ | |||
+ | {{dsc begin}} | ||
+ | {{dsc header|atomic}} | ||
+ | {{dsc h2|Atomic types}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_ref}} | ||
+ | {{dsc h2|Operations on atomic types}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_is_lock_free}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_store}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_load}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_exchange}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_compare_exchange}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_add}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_sub}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_and}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_or}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_xor}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_max}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_fetch_min}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_wait}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_notify_one}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_notify_all}} | ||
+ | {{dsc h2|Flag type and operations}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_test_and_set}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_clear}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_test}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_wait}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_notify_one}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_flag_notify_all}} | ||
+ | {{dsc h2|Initialization}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_init}} | ||
+ | {{dsc inc|cpp/atomic/dsc ATOMIC_VAR_INIT}} | ||
+ | {{dsc inc|cpp/atomic/dsc ATOMIC_FLAG_INIT}} | ||
+ | {{dsc h2|Memory synchronization ordering}} | ||
+ | {{dsc inc|cpp/atomic/dsc memory_order}} | ||
+ | {{dsc inc|cpp/atomic/dsc kill_dependency}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_thread_fence}} | ||
+ | {{dsc inc|cpp/atomic/dsc atomic_signal_fence}} | ||
+ | {{dsc header|stdatomic.h}} | ||
+ | {{dsc h2|C compatibility macros}} | ||
+ | {{dsc inc|cpp/atomic/dsc _Atomic}} | ||
+ | {{dsc end}} | ||
===Mutual exclusion=== | ===Mutual exclusion=== | ||
+ | Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads. | ||
− | + | {{dsc begin}} | |
+ | {{dsc header|mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc timed_mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc recursive_mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc recursive_timed_mutex}} | ||
+ | {{dsc header|shared_mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc shared_mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc shared_timed_mutex}} | ||
− | + | {{dsc h2|Generic mutex management}} | |
+ | {{dsc header|mutex}} | ||
+ | {{dsc inc|cpp/thread/dsc lock_guard}} | ||
+ | {{dsc inc|cpp/thread/dsc scoped_lock}} | ||
+ | {{dsc inc|cpp/thread/dsc unique_lock}} | ||
+ | {{dsc inc|cpp/thread/dsc shared_lock}} | ||
+ | {{dsc inc|cpp/thread/dsc lock_tag}} | ||
− | + | {{dsc h2|Generic locking algorithms}} | |
+ | {{dsc inc|cpp/thread/dsc try_lock}} | ||
+ | {{dsc inc|cpp/thread/dsc lock}} | ||
− | + | {{dsc h2|Call once}} | |
+ | {{dsc inc|cpp/thread/dsc once_flag}} | ||
+ | {{dsc inc|cpp/thread/dsc call_once}} | ||
+ | {{dsc end}} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===Condition variables=== | ===Condition variables=== | ||
+ | A condition variable is a synchronization primitive that allows multiple threads to communicate with each other. It allows some number of threads to wait (possibly with a timeout) for notification from another thread that they may proceed. A condition variable is always associated with a mutex. | ||
+ | |||
+ | {{dsc begin}} | ||
+ | {{dsc header|condition_variable}} | ||
+ | {{dsc inc|cpp/thread/dsc condition_variable}} | ||
+ | {{dsc inc|cpp/thread/dsc condition_variable_any}} | ||
+ | {{dsc inc|cpp/thread/dsc notify_all_at_thread_exit}} | ||
+ | {{dsc inc|cpp/thread/dsc cv_status}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{rrev|since=c++20| | ||
+ | ===Semaphores=== | ||
+ | A semaphore is a lightweight synchronization primitive used to constrain concurrent access to a shared resource. When either would suffice, a semaphore can be more efficient than a condition variable. | ||
+ | {{dsc begin}} | ||
+ | {{dsc header|semaphore}} | ||
+ | {{dsc inc|cpp/thread/dsc counting_semaphore}} | ||
+ | {{dsc inc|cpp/thread/dsc binary_semaphore}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | ===Latches and Barriers=== | ||
+ | Latches and barriers are thread coordination mechanisms that allow any number of threads to block until an expected number of threads arrive. A latch cannot be reused, while a barrier can be used repeatedly. | ||
+ | {{dsc begin}} | ||
+ | {{dsc header|latch}} | ||
+ | {{dsc inc|cpp/thread/dsc latch}} | ||
+ | {{dsc header|barrier}} | ||
+ | {{dsc inc|cpp/thread/dsc barrier}} | ||
+ | {{dsc end}} | ||
+ | }} | ||
+ | |||
+ | ===Futures=== | ||
+ | The standard library provides facilities to obtain values that are returned and to catch exceptions that are thrown by asynchronous tasks (i.e. functions launched in separate threads). These values are communicated in a ''shared state'', in which the asynchronous task may write its return value or store an exception, and which may be examined, waited for, and otherwise manipulated by other threads that hold instances of {{lc|std::future}} or {{lc|std::shared_future}} that reference that shared state. | ||
+ | |||
+ | {{dsc begin}} | ||
+ | {{dsc header|future}} | ||
+ | {{dsc inc|cpp/thread/dsc promise}} | ||
+ | {{dsc inc|cpp/thread/dsc packaged_task}} | ||
+ | {{dsc inc|cpp/thread/dsc future}} | ||
+ | {{dsc inc|cpp/thread/dsc shared_future}} | ||
+ | {{dsc inc|cpp/thread/dsc async}} | ||
+ | {{dsc inc|cpp/thread/dsc launch}} | ||
+ | {{dsc inc|cpp/thread/dsc future_status}} | ||
+ | |||
+ | {{dsc h2|Future errors}} | ||
+ | {{dsc inc|cpp/thread/dsc future_error}} | ||
+ | {{dsc inc|cpp/thread/dsc future_category}} | ||
+ | {{dsc inc|cpp/thread/dsc future_errc}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{rrev|since=c++26| | ||
+ | ===Safe Reclamation=== | ||
+ | Safe-reclamation techniques are most frequently used to straightforwardly resolve access-deletion races. | ||
+ | |||
+ | {{dsc begin}} | ||
+ | {{dsc h2|Read-Copy-Update Mechanism}} | ||
+ | {{dsc header|rcu}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_obj_base}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_domain}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_default_domain}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_synchronize}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_barrier}} | ||
+ | {{dsc inc|cpp/thread/dsc rcu_retire}} | ||
− | {{ | + | {{dsc h2|Hazard Pointers}} |
+ | {{dsc header|hazard_pointer}} | ||
+ | {{dsc inc|cpp/thread/dsc hazard_pointer_obj_base}} | ||
+ | {{dsc inc|cpp/thread/dsc hazard_pointer}} | ||
+ | {{dsc inc|cpp/thread/dsc make_hazard_pointer}} | ||
+ | {{dsc end}} | ||
+ | }} | ||
− | == | + | ===See also=== |
+ | {{dsc begin}} | ||
+ | {{dsc see c|c/thread|Concurrency support library|nomono=true}} | ||
+ | {{dsc end}} | ||
− | {{ | + | {{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 20:53, 8 November 2024
C++ includes built-in support for threads, atomic operations, mutual exclusion, condition variables, and futures.
Contents |
[edit] Threads
Threads enable programs to execute across several processor cores.
Defined in header
<thread> | |
(C++11) |
manages a separate thread (class) |
(C++20) |
std::thread with support for auto-joining and cancellation (class) |
Functions managing the current thread | |
Defined in namespace
this_thread | |
(C++11) |
suggests that the implementation reschedule execution of threads (function) |
(C++11) |
returns the thread id of the current thread (function) |
(C++11) |
stops the execution of the current thread for a specified time duration (function) |
(C++11) |
stops the execution of the current thread until a specified time point (function) |
Cooperative cancellationThe components stop source, stop token, and stop callback can be used to asynchronously request that an operation stops execution in a timely manner, typically because the result is no longer required. Such a request is called a stop request. These components specify the semantics of shared access to a stop state. Any object modeling any of these components that refer to the same stop state is an associated stop source, stop token, or stop callback, respectively.
They are designed:
In fact, they do not even need to be used to "stop" anything, but can instead be used for a thread-safe one-time function(s) invocation trigger, for example.
|
(since C++20) |
[edit] Cache size access
Defined in header
<new> | |
min offset to avoid false sharing max offset to promote true sharing (constant) |
[edit] Atomic operations
These components are provided for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object. Atomic objects are free of data races.
Neither the |
(since C++23) |
Defined in header
<atomic> | |
Atomic types | |
(C++11) |
atomic class template and specializations for bool, integral, floating-point,(since C++20) and pointer types (class template) |
(C++20) |
provides atomic operations on non-atomic objects (class template) |
Operations on atomic types | |
(C++11) |
checks if the atomic type's operations are lock-free (function template) |
(C++11)(C++11) |
atomically replaces the value of the atomic object with a non-atomic argument (function template) |
(C++11)(C++11) |
atomically obtains the value stored in an atomic object (function template) |
(C++11)(C++11) |
atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template) |
atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not (function template) | |
(C++11)(C++11) |
adds a non-atomic value to an atomic object and obtains the previous value of the atomic (function template) |
(C++11)(C++11) |
subtracts a non-atomic value from an atomic object and obtains the previous value of the atomic (function template) |
(C++11)(C++11) |
replaces the atomic object with the result of bitwise AND with a non-atomic argument and obtains the previous value of the atomic (function template) |
(C++11)(C++11) |
replaces the atomic object with the result of bitwise OR with a non-atomic argument and obtains the previous value of the atomic (function template) |
(C++11)(C++11) |
replaces the atomic object with the result of bitwise XOR with a non-atomic argument and obtains the previous value of the atomic (function template) |
(C++26)(C++26) |
replaces the atomic object with the result of std::max with a non-atomic argument and obtains the previous value of the atomic (function template) |
(C++26)(C++26) |
replaces the atomic object with the result of std::min with a non-atomic argument and obtains the previous value of the atomic (function template) |
(C++20)(C++20) |
blocks the thread until notified and the atomic value changes (function template) |
(C++20) |
notifies a thread blocked in atomic_wait (function template) |
(C++20) |
notifies all threads blocked in atomic_wait (function template) |
Flag type and operations | |
(C++11) |
the lock-free boolean atomic type (class) |
atomically sets the flag to true and returns its previous value (function) | |
(C++11)(C++11) |
atomically sets the value of the flag to false (function) |
(C++20)(C++20) |
atomically returns the value of the flag (function) |
(C++20)(C++20) |
blocks the thread until notified and the flag changes (function) |
(C++20) |
notifies a thread blocked in atomic_flag_wait (function) |
(C++20) |
notifies all threads blocked in atomic_flag_wait (function) |
Initialization | |
(C++11)(deprecated in C++20) |
non-atomic initialization of a default-constructed atomic object (function template) |
(C++11)(deprecated in C++20) |
constant initialization of an atomic variable of static storage duration (function macro) |
(C++11) |
initializes an std::atomic_flag to false (macro constant) |
Memory synchronization ordering | |
(C++11) |
defines memory ordering constraints for the given atomic operation (enum) |
(C++11) |
removes the specified object from the std::memory_order_consume dependency tree (function template) |
(C++11) |
generic memory order-dependent fence synchronization primitive (function) |
(C++11) |
fence between a thread and a signal handler executed in the same thread (function) |
Defined in header
<stdatomic.h> | |
C compatibility macros | |
(C++23) |
compatibility macro such that _Atomic(T) is identical to std::atomic<T> (function macro) |
[edit] Mutual exclusion
Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads.
Defined in header
<mutex> | |
(C++11) |
provides basic mutual exclusion facility (class) |
(C++11) |
provides mutual exclusion facility which implements locking with a timeout (class) |
(C++11) |
provides mutual exclusion facility which can be locked recursively by the same thread (class) |
(C++11) |
provides mutual exclusion facility which can be locked recursively by the same thread and implements locking with a timeout (class) |
Defined in header
<shared_mutex> | |
(C++17) |
provides shared mutual exclusion facility (class) |
(C++14) |
provides shared mutual exclusion facility and implements locking with a timeout (class) |
Generic mutex management | |
Defined in header
<mutex> | |
(C++11) |
implements a strictly scope-based mutex ownership wrapper (class template) |
(C++17) |
deadlock-avoiding RAII wrapper for multiple mutexes (class template) |
(C++11) |
implements movable mutex ownership wrapper (class template) |
(C++14) |
implements movable shared mutex ownership wrapper (class template) |
tags used to specify locking strategy (tag) | |
Generic locking algorithms | |
(C++11) |
attempts to obtain ownership of mutexes via repeated calls to try_lock (function template) |
(C++11) |
locks specified mutexes, blocks if any are unavailable (function template) |
Call once | |
(C++11) |
helper object to ensure that call_once invokes the function only once (class) |
(C++11) |
invokes a function only once even if called from multiple threads (function template) |
[edit] Condition variables
A condition variable is a synchronization primitive that allows multiple threads to communicate with each other. It allows some number of threads to wait (possibly with a timeout) for notification from another thread that they may proceed. A condition variable is always associated with a mutex.
Defined in header
<condition_variable> | |
(C++11) |
provides a condition variable associated with a std::unique_lock (class) |
(C++11) |
provides a condition variable associated with any lock type (class) |
(C++11) |
schedules a call to notify_all to be invoked when this thread is completely finished (function) |
(C++11) |
lists the possible results of timed waits on condition variables (enum) |
SemaphoresA semaphore is a lightweight synchronization primitive used to constrain concurrent access to a shared resource. When either would suffice, a semaphore can be more efficient than a condition variable.
Latches and BarriersLatches and barriers are thread coordination mechanisms that allow any number of threads to block until an expected number of threads arrive. A latch cannot be reused, while a barrier can be used repeatedly.
|
(since C++20) |
[edit] Futures
The standard library provides facilities to obtain values that are returned and to catch exceptions that are thrown by asynchronous tasks (i.e. functions launched in separate threads). These values are communicated in a shared state, in which the asynchronous task may write its return value or store an exception, and which may be examined, waited for, and otherwise manipulated by other threads that hold instances of std::future or std::shared_future that reference that shared state.
Defined in header
<future> | |
(C++11) |
stores a value for asynchronous retrieval (class template) |
(C++11) |
packages a function to store its return value for asynchronous retrieval (class template) |
(C++11) |
waits for a value that is set asynchronously (class template) |
(C++11) |
waits for a value (possibly referenced by other futures) that is set asynchronously (class template) |
(C++11) |
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result (function template) |
(C++11) |
specifies the launch policy for std::async (enum) |
(C++11) |
specifies the results of timed waits performed on std::future and std::shared_future (enum) |
Future errors | |
(C++11) |
reports an error related to futures or promises (class) |
(C++11) |
identifies the future error category (function) |
(C++11) |
identifies the future error codes (enum) |
Safe ReclamationSafe-reclamation techniques are most frequently used to straightforwardly resolve access-deletion races.
|
(since C++26) |
[edit] See also
C documentation for Concurrency support library
|