[atomics.types.operations]

# 32 Concurrency support library [[thread]](./#thread)

## 32.5 Atomic operations [[atomics]](atomics#types.operations)

### 32.5.8 Class template atomic [[atomics.types.generic]](atomics.types.generic#atomics.types.operations)

#### 32.5.8.2 Operations on atomic types [atomics.types.operations]

[ð](#lib:atomic,constructor)

`constexpr atomic() noexcept(is_nothrow_default_constructible_v<T>);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4538)

*Constraints*: is_default_constructible_v<T> is true[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4542)

*Effects*: Initializes the atomic object with the value of T()[.](#2.sentence-1)

Initialization is not an atomic operation ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"))[.](#2.sentence-2)

[ð](#lib:atomic,constructor_)

`constexpr atomic(T desired) noexcept;
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4557)

*Effects*: Initializes the object with the value desired[.](#3.sentence-1)

Initialization is not an atomic operation ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"))[.](#3.sentence-2)

[*Note [1](#note-1)*:

It is possible to have an access to an atomic object A race with its construction, for example by communicating the address of the
just-constructed object A to another thread viamemory_order​::​relaxed operations on a suitable atomic pointer
variable, and then immediately accessing A in the receiving thread[.](#3.sentence-3)

This results in undefined behavior[.](#3.sentence-4)

â *end note*]

[ð](#lib:is_always_lock_free,atomic)

`static constexpr bool is_always_lock_free = implementation-defined;
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4582)

The static data member is_always_lock_free is true if the atomic type's operations are always lock-free, and false otherwise[.](#4.sentence-1)

[*Note [2](#note-2)*:

The value of is_always_lock_free is consistent with the value of
the corresponding ATOMIC_..._LOCK_FREE macro, if defined[.](#4.sentence-2)

â *end note*]

[ð](#lib:atomic_is_lock_free)

`bool is_lock_free() const volatile noexcept;
bool is_lock_free() const noexcept;
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4604)

*Returns*: true if the object's operations are lock-free, false otherwise[.](#5.sentence-1)

[*Note [3](#note-3)*:

The return value of the is_lock_free member function
is consistent with the value of is_always_lock_free for the same type[.](#5.sentence-2)

â *end note*]

[ð](#lib:atomic_store)

`void store(T desired, memory_order order = memory_order::seq_cst) volatile noexcept;
constexpr void store(T desired, memory_order order = memory_order::seq_cst) noexcept;
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4625)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4630)

*Preconditions*: order ismemory_order​::​relaxed,memory_order​::​release, ormemory_order​::​seq_cst[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4637)

*Effects*: Atomically replaces the value pointed to by this with the value of desired[.](#8.sentence-1)

Memory is affected according to the value oforder[.](#8.sentence-2)

[ð](#lib:operator=,atomic)

`T operator=(T desired) volatile noexcept;
constexpr T operator=(T desired) noexcept;
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4654)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4659)

*Effects*: Equivalent to store(desired)[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4663)

*Returns*: desired[.](#11.sentence-1)

[ð](#lib:atomic_load)

`T load(memory_order order = memory_order::seq_cst) const volatile noexcept;
constexpr T load(memory_order order = memory_order::seq_cst) const noexcept;
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4680)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4685)

*Preconditions*: order ismemory_order​::​relaxed,memory_order​::​acquire, ormemory_order​::​seq_cst[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4692)

*Effects*: Memory is affected according to the value of order[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4696)

*Returns*: Atomically returns the value pointed to by this[.](#15.sentence-1)

[ð](#lib:operator_type,atomic)

`operator T() const volatile noexcept;
constexpr operator T() const noexcept;
`

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4711)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4716)

*Effects*: Equivalent to: return load();

[ð](#lib:atomic_exchange)

`T exchange(T desired, memory_order order = memory_order::seq_cst) volatile noexcept;
constexpr T exchange(T desired, memory_order order = memory_order::seq_cst) noexcept;
`

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4734)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#18.sentence-1)

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4739)

*Effects*: Atomically replaces the value pointed to by this with desired[.](#19.sentence-1)

Memory is affected according to the value of order[.](#19.sentence-2)

These operations are atomic read-modify-write operations ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"))[.](#19.sentence-3)

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4746)

*Returns*: Atomically returns the value pointed to by this immediately before the effects[.](#20.sentence-1)

[ð](#lib:atomic_compare_exchange_weak)

`bool compare_exchange_weak(T& expected, T desired,
                           memory_order success, memory_order failure) volatile noexcept;
constexpr bool compare_exchange_weak(T& expected, T desired,
                           memory_order success, memory_order failure) noexcept;
bool compare_exchange_strong(T& expected, T desired,
                             memory_order success, memory_order failure) volatile noexcept;
constexpr bool compare_exchange_strong(T& expected, T desired,
                             memory_order success, memory_order failure) noexcept;
bool compare_exchange_weak(T& expected, T desired,
                           memory_order order = memory_order::seq_cst) volatile noexcept;
constexpr bool compare_exchange_weak(T& expected, T desired,
                           memory_order order = memory_order::seq_cst) noexcept;
bool compare_exchange_strong(T& expected, T desired,
                             memory_order order = memory_order::seq_cst) volatile noexcept;
constexpr bool compare_exchange_strong(T& expected, T desired,
                             memory_order order = memory_order::seq_cst) noexcept;
`

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4783)

*Constraints*: For the volatile overload of this function,is_always_lock_free is true[.](#21.sentence-1)

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4788)

*Preconditions*: failure ismemory_order​::​relaxed,memory_order​::​acquire, ormemory_order​::​seq_cst[.](#22.sentence-1)

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4795)

*Effects*: Retrieves the value in expected[.](#23.sentence-1)

It then atomically
compares the value representation of the value pointed to by this for equality with that previously retrieved from expected,
and if true, replaces the value pointed to
by this with that in desired[.](#23.sentence-2)

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[.](#23.sentence-3)

When only one memory_order argument is
supplied, the value of success is order, and the value offailure is order except that a value of memory_order​::​acq_rel shall be replaced by the value memory_order​::​acquire and a value ofmemory_order​::​release shall be replaced by the valuememory_order​::​relaxed[.](#23.sentence-4)

If and only if the comparison is false then, after the atomic operation,
the value in expected is replaced by the value
pointed to by this during the atomic comparison[.](#23.sentence-5)

If the operation returns true, these
operations are atomic read-modify-write
operations ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races")) on the memory
pointed to by this[.](#23.sentence-6)

Otherwise, these operations are atomic load operations on that memory[.](#23.sentence-7)

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4819)

*Returns*: The result of the comparison[.](#24.sentence-1)

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4823)

[*Note [4](#note-4)*:

For example, the effect ofcompare_exchange_strong on objects without padding bits ([[basic.types.general]](basic.types.general#term.padding.bits "6.9.1 General")) isif (memcmp(this, &expected, sizeof(*this)) == 0) memcpy(this, &desired, sizeof(*this));else memcpy(&expected, this, sizeof(*this));

â *end note*]

[*Example [1](#example-1)*:

The expected use of the compare-and-exchange operations is as follows[.](#25.sentence-2)

The
compare-and-exchange operations will update expected when another iteration of
the loop is needed[.](#25.sentence-3)

expected = current.load();do { desired = function(expected);} while (!current.compare_exchange_weak(expected, desired)); â *end example*]

[*Example [2](#example-2)*:

Because the expected value is updated only on failure,
code releasing the memory containing the expected value on success will work[.](#25.sentence-4)

For example, list head insertion will act atomically and would not introduce a
data race in the following code:do { p->next = head; // make new list node point to the current head} while (!head.compare_exchange_weak(p->next, p)); // try to insert

â *end example*]

[26](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4858)

Implementations should ensure that weak compare-and-exchange operations do not
consistently return false unless either the atomic object has value
different from expected or there are concurrent modifications to the
atomic object[.](#26.sentence-1)

[27](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4864)

*Remarks*: A weak compare-and-exchange operation may fail spuriously[.](#27.sentence-1)

That is, even when
the contents of memory referred to by expected and this are
equal, it may return false and store back to expected the same memory
contents that were originally there[.](#27.sentence-2)

[*Note [5](#note-5)*:

This
spurious failure enables implementation of compare-and-exchange on a broader class of
machines, e.g., load-locked store-conditional machines[.](#27.sentence-3)

A
consequence of spurious failure is that nearly all uses of weak compare-and-exchange
will be in a loop[.](#27.sentence-4)

When a compare-and-exchange is in a loop, the weak version will yield better performance
on some platforms[.](#27.sentence-5)

When a weak compare-and-exchange would require a loop and a strong one
would not, the strong one is preferable[.](#27.sentence-6)

â *end note*]

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4881)

[*Note [6](#note-6)*:

Under cases where the memcpy and memcmp semantics of the compare-and-exchange
operations apply, the comparisons can fail for values that compare equal withoperator== if the value representation has trap bits or alternate
representations of the same value[.](#28.sentence-1)

Notably, on implementations conforming to
ISO/IEC 60559, floating-point -0.0 and +0.0 will not compare equal with memcmp but will compare equal with operator==,
and NaNs with the same payload will compare equal with memcmp but will not
compare equal with operator==[.](#28.sentence-2)

â *end note*]

[*Note [7](#note-7)*:

Because compare-and-exchange acts on an object's value representation,
padding bits that never participate in the object's value representation
are ignored[.](#28.sentence-3)

As a consequence, the following code is guaranteed to avoid
spurious failure:struct padded {char clank = 0x42; // Padding here.unsigned biff = 0xC0DEFEFE;};
atomic<padded> pad = {};

bool zap() { padded expected, desired{0, 0}; return pad.compare_exchange_strong(expected, desired);}

â *end note*]

[*Note [8](#note-8)*:

For a union with bits that participate in the value representation
of some members but not others, compare-and-exchange might always fail[.](#28.sentence-5)

This is because such padding bits have an indeterminate value when they
do not participate in the value representation of the active member[.](#28.sentence-6)

As a consequence, the following code is not guaranteed to ever succeed:union pony {double celestia = 0.; short luna; // padded};
atomic<pony> princesses = {};

bool party(pony desired) { pony expected; return princesses.compare_exchange_strong(expected, desired);}

â *end note*]

[ð](#lib:wait,atomic)

`void wait(T old, memory_order order = memory_order::seq_cst) const volatile noexcept;
constexpr void wait(T old, memory_order order = memory_order::seq_cst) const noexcept;
`

[29](#29)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4942)

*Preconditions*: order ismemory_order​::​relaxed,memory_order​::​acquire, ormemory_order​::​seq_cst[.](#29.sentence-1)

[30](#30)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4949)

*Effects*: Repeatedly performs the following steps, in order:

- [(30.1)](#30.1)

  Evaluates load(order) and
 compares its value representation for equality against that of old[.](#30.1.sentence-1)

- [(30.2)](#30.2)

  If they compare unequal, returns[.](#30.2.sentence-1)

- [(30.3)](#30.3)

  Blocks until it
 is unblocked by an atomic notifying operation or is unblocked spuriously[.](#30.3.sentence-1)

[31](#31)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4963)

*Remarks*: This function is an atomic waiting operation ([[atomics.wait]](atomics.wait "32.5.6 Waiting and notifying"))[.](#31.sentence-1)

[ð](#lib:notify_one,atomic)

`void notify_one() volatile noexcept;
constexpr void notify_one() noexcept;
`

[32](#32)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4978)

*Effects*: Unblocks the execution of at least one atomic waiting operation
that is eligible to be unblocked ([[atomics.wait]](atomics.wait "32.5.6 Waiting and notifying")) by this call,
if any such atomic waiting operations exist[.](#32.sentence-1)

[33](#33)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4984)

*Remarks*: This function is an atomic notifying operation ([[atomics.wait]](atomics.wait "32.5.6 Waiting and notifying"))[.](#33.sentence-1)

[ð](#lib:notify_all,atomic)

`void notify_all() volatile noexcept;
constexpr void notify_all() noexcept;
`

[34](#34)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L4999)

*Effects*: Unblocks the execution of all atomic waiting operations
that are eligible to be unblocked ([[atomics.wait]](atomics.wait "32.5.6 Waiting and notifying")) by this call[.](#34.sentence-1)

[35](#35)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L5004)

*Remarks*: This function is an atomic notifying operation ([[atomics.wait]](atomics.wait "32.5.6 Waiting and notifying"))[.](#35.sentence-1)
