Specializations of std::atomic_ref

Explore specializations associated with std::atomic_ref.

You can specialize std::atomic_ref for user-defined types, use partial specializations for pointer types, or full specializations for arithmetic types such as integral or floating-point types.

Primary template

The primary template std::atomic_ref can be instantiated with a TriviallyCopyable type T as:

struct Counters { 
    int a;
    int b; 
};
Counter counter;
std::atomic_ref<Counters> cnt(counter);

Partial specializations for pointer types

The standard provides partial specializations for a pointer type: std::atomic_ref<T*>.

Specializations for arithmetic types

The standard provides specialization for the integral and floating-point types: std::atomic_ref<arithmetic type>.

  • Character types: char, char8_t(C++20), char16_t, char32_t, and wchar_t

  • Standard signed-integer types: signed char, short, int, long, and long long

  • Standard unsigned-integer types: unsigned char, unsigned short, unsigned int, unsigned long, and unsigned long long

  • Additional integer types, defined in the header cstdint:

    • int8_t, int16_t, int32_t, and int64_t (signed integer with exactly 88, 1616, 3232, and 6464 bits)
    • uint8_t, uint16_t, uint32_t, and uint64_t (unsigned integer with exactly 88, 1616, 3232, and 6464 bits)
    • int_fast8_t, int_fast16_t, int_fast32_t, and int_fast64_t (fastest signed integer with at least 88, 1616, 3232, and 6464 bits)
    • uint_fast8_t, uint_fast16_t, uint_fast32_t, and uint_fast64_t (fastest unsigned integer with at least 88, 1616, 3232, and 6464 bits)
    • int_least8_t, int_least16_t, int_least32_t, and int_least64_t (smallest signed integer with at least 88, 1616, 3232, and 6464 bits)
    • uint_least8_t, uint_least16_t, uint_least32_t, and uint_least64_t (smallest unsigned integer with at least 88, 1616, 3232, and 6464 bits)
    • intmax_t, and uintmax_t (maximum signed and unsigned integer)
    • intptr_t, and uintptr_t (signed and unsigned integer for holding a pointer)
  • Standard floating-point types: float, double, and long double

All atomic operations

First, here is the list of all operations on atomic_ref:

Function Description
is_lock_free Checks if the atomic_ref object is lock-free.
load Automatically returns the value of the referenced object.
store Automatically replaces the value of the referenced object with a non-atomic.
exchange Automatically replaces the value of the referenced object with the new value.
compare_exchange_strong
compare_exchange_weak
Automatically compares and eventually exchanges the value of the referenced object.
fetch_add, +=
fetch_sub, -=
Automatically adds (subtracts) the value to (from) the referenced object.
fetch_or, |=
fetch_and, &=
fetch_xor, ^=
Automatically performs bitwise (AND, OR, and XOR) operation on the referenced object.
++, -- Increments or decrements (either pre- or post-increment) the referenced object.
notify_one
notify_all
Unblocks one atomic wait operation.
Unblocks all atomic wait operations.
wait Blocks until it is notified.
Compares itself with the old value to protect against spurious wakeupsA spurious wakeup is an erroneous notification. The waiting component of a condition variable or an atomic flag can get a notification, although the notification component didn’t send the signal. and lost wakeupsA lost wakeup is a situation in which a thread misses its wakeup notification due to a race condition..
If the value is different from the old value, returns.

Get hands-on with 1200+ tech skills courses.