std::atomic_ref(3)
π std::atomic_ref π
Okay bestie, so this whole page is from the C++ Standard Library manual, and we're about to get into std::atomic_ref β and honestly? It's giving "make any object go atomic" main character energy. Let's go. β¨
πΈ SYNOPSIS πΈ
Defined in header <atomic>
template< class T > (1) (since C++20)
struct atomic_ref;
template< class T > (2) (since C++20)
struct atomic_ref<T*>;
π¦ DESCRIPTION π¦
So the std::atomic_ref class template literally just applies atomic operations to the object it references. For the entire lifetime of the std::atomic_ref object, that referenced object is considered an atomic object. And if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (peep the memory model for the deets on data races). Also β accesses to atomic objects can establish inter-thread synchronization and can order non-atomic memory accesses exactly as std::memory_order specifies. Slay. π
The lifetime of an object must exceed the lifetime of all std::atomic_refs that reference it. And while any std::atomic_ref instance referencing an object exists, the object must be exclusively accessed through these std::atomic_ref instances. No subobject of an object referenced by an std::atomic_ref object may be concurrently referenced by any other std::atomic_ref object. Not me being strict, but fr fr, that's the rule. π
Atomic operations applied to an object through an std::atomic_ref are atomic with respect to atomic operations applied through any other std::atomic_ref referencing the same object. Iconic coordination. π€
std::atomic_ref is CopyConstructible. π
And like language references, constness is shallow for std::atomic_ref β which means you can modify the referenced value through a const std::atomic_ref object. Yes bestie, even a const one can change the thing it points to. Plot twist. π
π SPECIALIZATIONS π
Primary template
The primary std::atomic_ref template may be instantiated with any TriviallyCopyable type T (including bool):
struct Counters { int a; int b; }; // user-defined trivially-copyable type
alignas(std::atomic_ref<Counters>::required_alignment) Counters counter;
std::atomic_ref<Counters> cnt(counter); // specialization for the user-defined type
Partial specialization for pointer types
The standard library provides partial specializations of the std::atomic_ref template for all pointer types. On top of the operations provided for all atomic types, these specializations additionally support atomic arithmetic operations appropriate to pointer types, such as fetch_add, fetch_sub. Pointer math but make it atomic. π
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:
- The character types
char,char8_t,char16_t,char32_t, andwchar_t; - The standard signed integer types:
signed char,short,int,long, andlong long; - The standard unsigned integer types:
unsigned char,unsigned short,unsigned int,unsigned long, andunsigned long long; - Any additional integral types needed by the typedefs in the header
<cstdint>.
Signed integer arithmetic is defined to use two's complement; there are no undefined results. No cap β two's complement all the way. π₯
Specializations for floating-point types
When instantiated with one of the cv-unqualified floating-point types (float, double, long double and cv-unqualified extended floating-point types (since C++23)), 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 TYPES π
| Member type | Definition |
|---|---|
value_type | see below |
difference_type | value_type (only for atomic_ref<Integral> and atomic_ref<Floating> specializations) |
std::ptrdiff_t (only for std::atomic_ref<T*> 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.
β¨ MEMBER FUNCTIONS β¨
constructorβ constructs anatomic_refobject (public member function)operator=β stores a value into the object referenced by anatomic_refobject (public member function)is_lock_freeβ checks if theatomic_refobject is lock-free (public member function)storeβ atomically replaces the value of the referenced object with a non-atomic argument (public member function)loadβ atomically obtains the value of the referenced object (public member function)operator Tβ loads a value from the referenced object (public member function)exchangeβ atomically replaces the value of the referenced object and obtains the value held previously (public member function)compare_exchange_weak,compare_exchange_strongβ atomically compares the value of the referenced object with a non-atomic argument and performs atomic exchange if equal or atomic load if not (public member function)waitβ blocks the thread until notified and the atomic value changes (public member function)notify_oneβ notifies at least one thread waiting on the atomic object (public member function)notify_allβ notifies all threads blocked waiting on the atomic object (public member function)
π CONSTANTS π
is_always_lock_free[static]β indicates that the type is always lock-free (public static member constant)required_alignment[static]β indicates the required alignment of an object to be referenced byatomic_ref(public static member constant)
π«Ά SPECIALIZED MEMBER FUNCTIONS π«Ά
Specialized for integral, floating-point and pointer types
fetch_addβ atomically adds the argument to the value stored in the referenced object and obtains the value held previously (public member function)fetch_subβ atomically subtracts the argument from the value stored in the referenced object and obtains the value held previously (public member function)operator+=,operator-=β atomically adds to or subtracts from the referenced value (public member function)
Specialized for integral and pointer types only
fetch_max(C++26)β atomically performsstd::maxbetween the argument and the value of the referenced object and obtains the value held previously (public member function)fetch_min(C++26)β atomically performsstd::minbetween the argument and the value of the referenced object and obtains the value held previously (public member function)operator++,operator++(int),operator--,operator--(int)β atomically increments or decrements the referenced object by one (public member function)
Specialized for integral types only
fetch_andβ atomically performs bitwise AND between the argument and the value of the referenced object and obtains the value held previously (public member function)fetch_orβ atomically performs bitwise OR between the argument and the value of the referenced object and obtains the value held previously (public member function)fetch_xorβ atomically performs bitwise XOR between the argument and the value of the referenced object and obtains the value held previously (public member function)operator&=,operator|=,operator^=β atomically performs bitwise AND, OR, XOR with the referenced value (public member function)
π NOTES π
Feature-test macro check bestie:
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_atomic_ref | 201806L | (C++20) | std::atomic_ref |
π SEE ALSO π
atomic(C++11)β atomic class template and specializations forbool, integral, floating-point, (since C++20) and pointer types (class template)
This page is literally brought to you byhttp://cppreference.comand the date2024.06.10, so if you ever need to cite your sources, that's right there. Periodt. πβ¨
Stay atomic, bestie β don't let those data races catch you slippin'. π π₯π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms