std::atomic (3) - Linux Manuals
std::atomic: std::atomic
NAME
Synopsis
Defined in header <atomic>
template< class T > (1) (since C++11)
struct atomic;
template< class T > (2) (since C++11)
struct atomic<T*>;
Defined in header <memory>
template<class T> (3) (since C++20)
struct atomic<std::shared_ptr<T>>;
template<class T> (4) (since C++20)
struct atomic<std::weak_ptr<T>>;
Each instantiation and full specialization of the std::atomic template defines an atomic type. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory_model for details on data races)
In addition, accesses to atomic objects may establish inter-thread synchronization and order non-atomic memory accesses as specified by std::memory_order.
std::atomic is neither copyable nor movable.
Specializations
Primary template
The primary std::atomic template may be instantiated with any TriviallyCopyable type T satisfying both CopyConstructible and CopyAssignable. The program is ill-formed if any of following values is false:
* std::is_trivially_copyable<T>::value
* std::is_copy_constructible<T>::value
* std::is_move_constructible<T>::value
* std::is_copy_assignable<T>::value
* std::is_move_assignable<T>::value
std::atomic<bool> uses the primary template. It is guaranteed to be a standard layout struct.
Partial specializations
The standard library provides partial specializations of the std::atomic template for the following types with additional properties that the primary template does not have:
2) Partial specializations std::atomic<T*> for all pointer types. These specializations have standard layout, trivial default constructors, and trivial destructors. Besides the operations provided for all atomic types, these specializations additionally support atomic arithmetic operations appropriate to pointer types, such as fetch_add, fetch_sub.
3-4) Partial specializations std::atomic<std::shared_ptr<T>> and std::atomic<std::weak_ptr<T>> are provided for std::shared_ptr and std::weak_ptr. (since C++20)
See std::atomic<std::shared_ptr> and std::atomic<std::weak_ptr> for details.
Specializations for integral types
When instantiated with one of the following integral types, std::atomic provides additional atomic operations appropriate to integral types such as fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor:
Additionally, the resulting std::atomic<Integral> specialization has standard layout, a trivial default constructor, and a trivial destructor. Signed integer arithmetic is defined to use two's complement; there are no undefined results.
Specializations for floating-point types