29 Atomic operations library [atomics]

29.6 Class template atomic_­ref [atomics.ref.generic]

29.6.1 Operations [atomics.ref.operations]

static constexpr bool is_always_lock_free;
The static data member is_­always_­lock_­free is true if the atomic_­ref type's operations are always lock-free, and false otherwise.
static constexpr size_t required_alignment;
The alignment required for an object to be referenced by an atomic reference, which is at least alignof(T).
[ Note
:
Hardware could require an object referenced by an atomic_­ref to have stricter alignment ([basic.align]) than other objects of type T.
Further, whether operations on an atomic_­ref are lock-free could depend on the alignment of the referenced object.
For example, lock-free operations on std​::​complex<double> could be supported only if aligned to 2*alignof(double).
— end note
 ]
atomic_ref(T& obj);
Requires: The referenced object shall be aligned to required_­alignment.
Effects: Constructs an atomic reference that references the object.
Throws: Nothing.
atomic_ref(const atomic_ref& ref) noexcept;
Effects: Constructs an atomic reference that references the object referenced by ref.
T operator=(T desired) const noexcept;
Effects: Equivalent to:
store(desired);
return desired;
operator T() const noexcept;
Effects: Equivalent to: return load();
bool is_lock_free() const noexcept;
Returns: true if the object's operations are lock-free, false otherwise.
void store(T desired, memory_order order = memory_order_seq_cst) const noexcept;
Requires: The order argument shall not be memory_­order_­consume, memory_­order_­acquire, nor memory_­order_­acq_­rel.
Effects: Atomically replaces the value referenced by *ptr with the value of desired.
Memory is affected according to the value of order.
T load(memory_order order = memory_order_seq_cst) const noexcept;
Requires: The order argument shall not be memory_­order_­release nor memory_­order_­acq_­rel.
Effects: Memory is affected according to the value of order.
Returns: Atomically returns the value referenced by *ptr.
T exchange(T desired, memory_order order = memory_order_seq_cst) const noexcept;
Effects: Atomically replaces the value referenced by *ptr with desired.
Memory is affected according to the value of order.
This operation is an atomic read-modify-write operation ([intro.multithread]).
Returns: Atomically returns the value referenced by *ptr immediately before the effects.
bool compare_exchange_weak(T& expected, T desired, memory_order success, memory_order failure) const noexcept; bool compare_exchange_strong(T& expected, T desired, memory_order success, memory_order failure) const noexcept; bool compare_exchange_weak(T& expected, T desired, memory_order order = memory_order_seq_cst) const noexcept; bool compare_exchange_strong(T& expected, T desired, memory_order order = memory_order_seq_cst) const noexcept;
Requires: The failure argument shall not be memory_­order_­release nor memory_­order_­acq_­rel.
Effects: Retrieves the value in expected.
It then atomically compares the value referenced by *ptr for equality with that previously retrieved from expected, and if true, replaces the value referenced by *ptr with that in desired.
If and only if the comparison is true, memory is affected according to the value of success, and if the comparison is false, memory is affected according to the value of failure.
When only one memory_­order argument is supplied, the value of success is order, and the value of failure is order except that a value of memory_­order_­acq_­rel shall be replaced by the value memory_­order_­acquire and a value of memory_­order_­release shall be replaced by the value memory_­order_­relaxed.
If and only if the comparison is false then, after the atomic operation, the contents of the memory in expected are replaced by the value read from the value referenced by *ptr during the atomic comparison.
If the operation returns true, these operations are atomic read-modify-write operations ([intro.races]) on the value referenced by *ptr.
Otherwise, these operations are atomic load operations on that memory.
Returns: The result of the comparison.
Remarks: A weak compare-and-exchange operation may fail spuriously.
That is, even when the contents of memory referred to by expected and ptr are equal, it may return false and store back to expected the same memory contents that were originally there.
[ Note
:
This spurious failure enables implementation of compare-and-exchange on a broader class of machines, e.g., load-locked store-conditional machines. A consequence of spurious failure is that nearly all uses of weak compare-and-exchange will be in a loop. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.
— end note
 ]