std::atomic_ref (3) - Linux Man Pages
Defined in header <atomic>
template< class T > (1) (since C++20)
template< class T > (2) (since C++20)
The std::atomic_ref class template applies atomic operations to the object it references. For the lifetime of the atomic_ref object, the object it references is considered an atomic object. 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.
The lifetime of an object must exceed the lifetime of all atomic_refs that references the object. While any atomic_ref instances referencing an object exists, the object must be exclusively accessed through these atomic_ref instances. No subobject of an object referenced by an atomic_ref object may be concurrently referenced by any other atomic_ref object.
Atomic operations applied to an object through an atomic_ref are atomic with respect to atomic operations applied through any other atomic_ref referencing the same object.
std::atomic_ref is CopyConstructible.
Like language references, constness is shallow for atomic_ref - it is possible to modify the referenced value through a const atomic_ref object.
The primary std::atomic_ref template may be instantiated with any TriviallyCopyable type T (including bool):
Partial specialization for pointer types
The standard library provides partial specializations of the std::atomic_ref template for for all pointer types. In addition to the operations provided for all atomic types, these specializations additionally support atomic arithmetic operations appropriate to pointer types, such as fetch_add, fetch_sub.
Specializations for integral types
When instantiated with one of the following integral types, std::atomic_ref provides additional atomic operations appropriate to integral types such as fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor:
Signed integer arithmetic is defined to use two's complement; there are no undefined results.
Specializations for floating-point types
When instantiated with one of the floating-point types float, double, and long double, std::atomic_ref provides additional atomic operations appropriate to floating-point types such as fetch_add and fetch_sub.
No operations result in undefined behavior even if the result is not representable in the floating-point type. The floating-point_environment in effect may be different from the calling thread's floating-point environment.
Member type Definition
value_type see below
difference_type value_type (only for atomic_ref<Integral> and atomic_ref<Floating> specializations)
For every std::atomic_ref<X> (whether or not specialized), std::atomic_ref<X>::value_type is X.
difference_type is not defined in the primary atomic_ref template.
constructor (public member function)
operator= (public member function)
is_lock_free (public member function)
store (public member function)
load (public member function)
operator_T (public member function)
exchange (public member function)
compare_exchange_weak (public member function)
is_always_lock_free indicates that the type is always lock-free