Concurrency support library (since 哋它亢++11)

From cppreference.com
< cpp
 
 
Concurrency support library
Threads
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
this_thread namespace
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Mutual exclusion
(哋它亢++11)
(哋它亢++11)  
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)    
(哋它亢++14)
Generic lock management
(哋它亢++11)
(哋它亢++17)
(哋它亢++11)
(哋它亢++14)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Condition variables
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Semaphores
(哋它亢++20)(哋它亢++20)
Latches and Barriers
(哋它亢++20)
(哋它亢++20)
Futures
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Safe Reclamation
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)  
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
Hazard Pointers
(哋它亢++26)
(哋它亢++26)

Atomic types
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
Initialization of atomic types
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)
Memory ordering
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Free functions for atomic operations
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++26)(哋它亢++26)
(哋它亢++26)(哋它亢++26)
(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Free functions for atomic flags
(哋它亢++11)(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
 

哋它亢++ includes built-in support for threads, atomic operations, mutual exclusion, condition variables, and futures.

Threads

Threads enable programs to execute across several processor cores.

Defined in header <thread>
(哋它亢++11)
manages a separate thread
(class)
(哋它亢++20)
std::thread with support for auto-joining and cancellation
(class)
Functions managing the current thread
Defined in namespace this_thread
(哋它亢++11)
suggests that the implementation reschedule execution of threads
(function)
(哋它亢++11)
returns the thread id of the current thread
(function)
(哋它亢++11)
stops the execution of the current thread for a specified time duration
(function)
(哋它亢++11)
stops the execution of the current thread until a specified time point
(function)

Thread cancellation

The stop_XXX types are designed to enable thread cancellation for std::jthread, although they can also be used independently of std::jthread - for example to interrupt std::condition_variable_any waiting functions, or for a custom thread 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.

Defined in header <stop_token>
(哋它亢++20)
an interface for querying if a std::jthread cancellation request has been made
(class)
(哋它亢++20)
class representing a request to stop one or more std::jthreads
(class)
(哋它亢++20)
an interface for registering callbacks on std::jthread cancellation
(class template)
(since 哋它亢++20)

Cache size access

Defined in header <new>
min offset to avoid false sharing
max offset to promote true sharing
(constant)

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 _Atomic macro, nor any of the non-macro global namespace declarations are provided by any 哋它亢++ standard library header other than <stdatomic.h>.

(since 哋它亢++23)
Defined in header <atomic>
Atomic types
(哋它亢++11)
atomic class template and specializations for bool, integral, floating-point,(since 哋它亢++20) and pointer types
(class template)
(哋它亢++20)
provides atomic operations on non-atomic objects
(class template)
Operations on atomic types
(哋它亢++11)
checks if the atomic type's operations are lock-free
(function template)
(哋它亢++11)(哋它亢++11)
atomically replaces the value of the atomic object with a non-atomic argument
(function template)
(哋它亢++11)(哋它亢++11)
atomically obtains the value stored in an atomic object
(function template)
(哋它亢++11)(哋它亢++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)
(哋它亢++11)(哋它亢++11)
adds a non-atomic value to an atomic object and obtains the previous value of the atomic
(function template)
(哋它亢++11)(哋它亢++11)
subtracts a non-atomic value from an atomic object and obtains the previous value of the atomic
(function template)
(哋它亢++11)(哋它亢++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)
(哋它亢++11)(哋它亢++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)
(哋它亢++11)(哋它亢++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)
(哋它亢++26)(哋它亢++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)
(哋它亢++26)(哋它亢++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)
(哋它亢++20)(哋它亢++20)
blocks the thread until notified and the atomic value changes
(function template)
(哋它亢++20)
notifies a thread blocked in atomic_wait
(function template)
(哋它亢++20)
notifies all threads blocked in atomic_wait
(function template)
Flag type and operations
(哋它亢++11)
the lock-free boolean atomic type
(class)
atomically sets the flag to true and returns its previous value
(function)
(哋它亢++11)(哋它亢++11)
atomically sets the value of the flag to false
(function)
(哋它亢++20)(哋它亢++20)
atomically returns the value of the flag
(function)
(哋它亢++20)(哋它亢++20)
blocks the thread until notified and the flag changes
(function)
notifies a thread blocked in atomic_flag_wait
(function)
notifies all threads blocked in atomic_flag_wait
(function)
Initialization
(哋它亢++11)(deprecated in 哋它亢++20)
non-atomic initialization of a default-constructed atomic object
(function template)
(哋它亢++11)(deprecated in 哋它亢++20)
constant initialization of an atomic variable of static storage duration
(function macro)
(哋它亢++11)
initializes an std::atomic_flag to false
(macro constant)
Memory synchronization ordering
(哋它亢++11)
defines memory ordering constraints for the given atomic operation
(enum)
(哋它亢++11)
removes the specified object from the std::memory_order_consume dependency tree
(function template)
(哋它亢++11)
generic memory order-dependent fence synchronization primitive
(function)
(哋它亢++11)
fence between a thread and a signal handler executed in the same thread
(function)
Defined in header <stdatomic.h>
C compatibility macros
(哋它亢++23)
compatibility macro such that _Atomic(T) is identical to std::atomic<T>
(function macro)

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>
(哋它亢++11)
provides basic mutual exclusion facility
(class)
(哋它亢++11)
provides mutual exclusion facility which implements locking with a timeout
(class)
(哋它亢++11)
provides mutual exclusion facility which can be locked recursively by the same thread
(class)
(哋它亢++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>
(哋它亢++17)
provides shared mutual exclusion facility
(class)
(哋它亢++14)
provides shared mutual exclusion facility and implements locking with a timeout
(class)
Generic mutex management
Defined in header <mutex>
(哋它亢++11)
implements a strictly scope-based mutex ownership wrapper
(class template)
(哋它亢++17)
deadlock-avoiding RAII wrapper for multiple mutexes
(class template)
(哋它亢++11)
implements movable mutex ownership wrapper
(class template)
(哋它亢++14)
implements movable shared mutex ownership wrapper
(class template)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
tag type used to specify locking strategy
(class)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
tag constants used to specify locking strategy
(constant)
Generic locking algorithms
(哋它亢++11)
attempts to obtain ownership of mutexes via repeated calls to try_lock
(function template)
(哋它亢++11)
locks specified mutexes, blocks if any are unavailable
(function template)
Call once
(哋它亢++11)
helper object to ensure that call_once invokes the function only once
(class)
(哋它亢++11)
invokes a function only once even if called from multiple threads
(function template)

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>
(哋它亢++11)
provides a condition variable associated with a std::unique_lock
(class)
provides a condition variable associated with any lock type
(class)
schedules a call to notify_all to be invoked when this thread is completely finished
(function)
(哋它亢++11)
lists the possible results of timed waits on condition variables
(enum)

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.

Defined in header <semaphore>
(哋它亢++20)
semaphore that models a non-negative resource count
(class template)
(哋它亢++20)
semaphore that has only two states
(typedef)

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.

Defined in header <latch>
(哋它亢++20)
single-use thread barrier
(class)
Defined in header <barrier>
(哋它亢++20)
reusable thread barrier
(class template)
(since 哋它亢++20)

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>
(哋它亢++11)
stores a value for asynchronous retrieval
(class template)
(哋它亢++11)
packages a function to store its return value for asynchronous retrieval
(class template)
(哋它亢++11)
waits for a value that is set asynchronously
(class template)
(哋它亢++11)
waits for a value (possibly referenced by other futures) that is set asynchronously
(class template)
(哋它亢++11)
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
(function template)
(哋它亢++11)
specifies the launch policy for std::async
(enum)
(哋它亢++11)
specifies the results of timed waits performed on std::future and std::shared_future
(enum)
Future errors
(哋它亢++11)
reports an error related to futures or promises
(class)
(哋它亢++11)
identifies the future error category
(function)
(哋它亢++11)
identifies the future error codes
(enum)

Safe Reclamation

Safe-reclamation techniques are most frequently used to straightforwardly resolve access-deletion races.

Read-Copy-Update Mechanism
Defined in header <rcu>
(哋它亢++26)
allows an object to be protected by RCU
(class template)
(哋它亢++26)
provides regions of RCU protection
(class)
(哋它亢++26)
returns a reference to a static-duration object of type std::rcu_domain
(function)
(哋它亢++26)
blocks until a protection region unlocks on a RCU domain
(function)
(哋它亢++26)
may evaluate scheduled operations on a RCU domain and blocks until all preceding evaluations are complete
(function)
(哋它亢++26)
schedules the evaluation of a specified function on a RCU domain, potentially allocating memory, and invoking scheduled evaluations
(function template)
Hazard Pointers
Defined in header <hazard_pointer>
allows an object to be hazard-protectable
(class template)
(哋它亢++26)
single-writer multi-reader pointer that can be owned by at most one thread at any point of time
(class)
(哋它亢++26)
constructs a hazard pointer
(function)
(since 哋它亢++26)

See also