atomic
Structure
Describes an object that performs atomic
operations on a stored value of type Ty
.
template <class Ty>
struct atomic;
Member | Description |
---|---|
Constructor | |
atomic |
Constructs an atomic object. |
Operators | |
atomic::operator Ty |
Reads and returns the stored value. (atomic::load ) |
atomic::operator= |
Uses a specified value to replace the stored value. (atomic::store ) |
atomic::operator++ |
Increments the stored value. Used only by integral and pointer specializations. |
atomic::operator+= |
Adds a specified value to the stored value. Used only by integral and pointer specializations. |
atomic::operator-- |
Decrements the stored value. Used only by integral and pointer specializations. |
atomic::operator-= |
Subtracts a specified value from the stored value. Used only by integral and pointer specializations. |
atomic::operator&= |
Performs a bitwise "and" (& ) on a specified value and the stored value. Used only by integral specializations. |
atomic::operator|= |
Performs a bitwise "or" (| ) on a specified value and the stored value. Used only by integral specializations. |
atomic::operator^= |
Performs a bitwise "exclusive or" (^ ) on a specified value and the stored value. Used only by integral specializations. |
Functions | |
compare_exchange_strong |
Performs an atomic_compare_and_exchange operation on this and returns the result. |
compare_exchange_weak |
Performs a weak_atomic_compare_and_exchange operation on this and returns the result. |
fetch_add |
Adds a specified value to the stored value. |
fetch_and |
Performs a bitwise "and" (& ) on a specified value and the stored value. |
fetch_or |
Performs a bitwise "or" (| ) on a specified value and the stored value. |
fetch_sub |
Subtracts a specified value from the stored value. |
fetch_xor |
Performs a bitwise "exclusive or" (^ ) on a specified value and the stored value. |
is_lock_free |
Specifies whether atomic operations on this are lock free. An atomic type is lock free if no atomic operations on that type use locks. |
load |
Reads and returns the stored value. |
store |
Uses a specified value to replace the stored value. |
The type Ty
must be trivially copyable. That is, using memcpy
to copy its bytes must produce a valid Ty
object that compares equal to the original object. The compare_exchange_weak
and compare_exchange_strong
member functions use memcmp
to determine whether two Ty
values are equal. These functions won't use a Ty
-defined operator==
. The member functions of atomic
use memcpy
to copy values of type Ty
.
A partial specialization, atomic<Ty*>
, exists for all pointer types. The specialization enables the addition of an offset to the managed pointer value or the subtraction of an offset from it. The arithmetic operations take an argument of type ptrdiff_t
and adjust that argument according to the size of Ty
to be consistent with ordinary address arithmetic.
A specialization exists for every integral type except bool
. Each specialization provides a rich set of methods for atomic arithmetic and logical operations.
atomic<char>
atomic<signed char>
atomic<unsigned char>
atomic<char16_t>
atomic<char32_t>
atomic<wchar_t>
atomic<short>
atomic<unsigned short>
atomic<int>
atomic<unsigned int>
atomic<long>
atomic<unsigned long>
atomic<long long>
atomic<unsigned long long>
Integral specializations are derived from corresponding atomic_integral
types. For example, atomic<unsigned int>
is derived from atomic_uint
.
Header: <atomic>
Namespace: std
Constructs an atomic object.
atomic();
atomic( const atomic& );
atomic( Ty Value ) noexcept;
Value
Initialization value.
Atomic objects can't be copied or moved.
Objects that are instantiations of atomic<Ty>
can be initialized only by the constructor that takes an argument of type Ty
and not by using aggregate initialization. However, atomic_integral
objects can be initialized only by using aggregate initialization.
atomic<int> ai0 = ATOMIC_VAR_INIT(0);
atomic<int> ai1(0);
The operator for the type specified to the template, atomic<Ty>
. Retrieves the stored value in *this
.
atomic<Ty>::operator Ty() const volatile noexcept;
atomic<Ty>::operator Ty() const noexcept;
This operator applies the memory_order_seq_cst
memory_order
.
Stores a specified value.
Ty operator=(
Ty Value
) volatile noexcept;
Ty operator=(
Ty Value
) noexcept;
Value
A Ty
object.
Returns Value
.
Increments the stored value. Used only by integral and pointer specializations.
Ty atomic<Ty>::operator++(int) volatile noexcept;
Ty atomic<Ty>::operator++(int) noexcept;
Ty atomic<Ty>::operator++() volatile noexcept;
Ty atomic<Ty>::operator++() noexcept;
The first two operators return the incremented value; the last two operators return the value before the increment. The operators use the memory_order_seq_cst
memory_order
.
Adds a specified value to the stored value. Used only by integral and pointer specializations.
Ty atomic<Ty>::operator+=(
Ty Value
) volatile noexcept;
Ty atomic<Ty>::operator+=(
Ty Value
) noexcept;
Value
An integral or pointer value.
A Ty
object that contains the result of the addition.
This operator uses the memory_order_seq_cst
memory_order
.
Decrements the stored value. Used only by integral and pointer specializations.
Ty atomic<Ty>::operator--(int) volatile noexcept;
Ty atomic<Ty>::operator--(int) noexcept;
Ty atomic<Ty>::operator--() volatile noexcept;
Ty atomic<Ty>::operator--() noexcept;
The first two operators return the decremented value; the last two operators return the value before the decrement. The operators use the memory_order_seq_cst
memory_order
.
Subtracts a specified value from the stored value. Used only by integral and pointer specializations.
Ty atomic<Ty>::operator-=(
Ty Value
) volatile noexcept;
Ty atomic<Ty>::operator-=(
Ty Value
) noexcept;
Value
An integral or pointer value.
A Ty
object that contains the result of the subtraction.
This operator uses the memory_order_seq_cst
memory_order
.
Performs a bitwise "and" (&
) on a specified value and the stored value of *this
. Used only by integral specializations.
atomic<Ty>::operator&= (
Ty Value
) volatile noexcept;
atomic<Ty>::operator&= (
Ty Value
) noexcept;
Value
A value of type Ty
.
The result of the bitwise "and" (&
).
This operator performs a read-modify-write operation to replace the stored value of *this
with a bitwise "and" (&
) of Value
and the current value that is stored in *this
, within the constraints of the memory_order_seq_cst
memory_order
.
Performs a bitwise "or" (|
) on a specified value and the stored value of *this
. Used only by integral specializations.
atomic<Ty>::operator|= (
Ty Value
) volatile noexcept;
atomic<Ty>::operator|= (
Ty Value
) noexcept;
Value
A value of type Ty
.
The result of the bitwise "or" (|
).
This operator performs a read-modify-write operation to replace the stored value of *this
with a bitwise "or" (|
) of Value
and the current value that is stored in *this
, within the constraints of the memory_order_seq_cst
memory_order
constraints.
Performs a bitwise "exclusive or" (^
) on a specified value and the stored value of *this
. Used only by integral specializations.
atomic<Ty>::operator^= (
Ty Value
) volatile noexcept;
atomic<Ty>::operator^= (
Ty Value
) noexcept;
Value
A value of type Ty
.
The result of the bitwise "exclusive or" (^
).
This operator performs a read-modify-write operation to replace the stored value of *this
with a bitwise "exclusive or" (^
) of Value
and the current value that is stored in *this
, within the constraints of the memory_order_seq_cst
memory_order
constraints.
Performs an atomic compare and exchange operation on *this
.
bool compare_exchange_strong(
Ty& Exp,
Ty Value,
memory_order Order1,
memory_order Order2
) volatile noexcept;
bool compare_exchange_strong(
Ty& Exp,
Ty Value,
memory_order Order1,
memory_order Order2
) noexcept;
bool compare_exchange_strong(
Ty& Exp,
Ty Value,
memory_order Order1 = memory_order_seq_cst
) volatile noexcept;
bool compare_exchange_strong(
Ty& Exp,
Ty Value,
memory_order Order1 = memory_order_seq_cst
) noexcept;
Exp
A value of type Ty
.
Value
A value of type Ty
.
Order1
First memory_order
argument.
Order2
Second memory_order
argument.
A bool
that indicates the result of the value comparison.
This atomic compare and exchange operation compares the value that is stored in *this
with Exp
. If the values are equal, the operation replaces the value that is stored in *this
with Value
by using a read-modify-write operation and applying the memory order constraints that are specified by Order1
. If the values aren't equal, the operation uses the value that is stored in *this
to replace Exp
and applies the memory order constraints that are specified by Order2
.
Overloads that don't have a second memory_order
use an implicit Order2
that is based on the value of Order1
. If Order1
is memory_order_acq_rel
, Order2
is memory_order_acquire
. If Order1
is memory_order_release
, Order2
is memory_order_relaxed
. In all other cases, Order2
is equal to Order1
.
For overloads that take two memory_order
parameters, the value of Order2
must not be memory_order_release
or memory_order_acq_rel
, and must not be stronger than the value of Order1
.
Performs a weak atomic compare and exchange operation on *this
.
bool compare_exchange_weak(
Ty& Exp,
Ty Value,
memory_order Order1,
memory_order Order2
) volatile noexcept;
bool compare_exchange_weak(
Ty& Exp,
Ty Value,
memory_order Order1,
memory_order Order2
) noexcept;
bool compare_exchange_weak(
Ty& Exp,
Ty Value,
memory_order Order1 = memory_order_seq_cst
) volatile noexcept;
bool compare_exchange_weak(
Ty& Exp,
Ty Value,
memory_order Order1 = memory_order_seq_cst
) noexcept;
Exp
A value of type Ty
.
Value
A value of type Ty
.
Order1
First memory_order
argument.
Order2
Second memory_order
argument.
A bool
that indicates the result of the value comparison.
This atomic compare and exchange operation compares the value that is stored in *this
with Exp
. If the values are equal, the operation replaces the value that is stored in *this
with Value
by using a read-modify-write operation and applying the memory order constraints that are specified by Order1
. If the values aren't equal, the operation uses the value that is stored in *this
to replace Exp
and applies the memory order constraints that are specified by Order2
.
A weak atomic compare and exchange operation performs an exchange if the compared values are equal. If the values aren't equal, the operation isn't guaranteed to perform an exchange.
Overloads that don't have a second memory_order
use an implicit Order2
that is based on the value of Order1
. If Order1
is memory_order_acq_rel
, Order2
is memory_order_acquire
. If Order1
is memory_order_release
, Order2
is memory_order_relaxed
. In all other cases, Order2
is equal to Order1
.
For overloads that take two memory_order
parameters, the value of Order2
must not be memory_order_release
or memory_order_acq_rel
, and must not be stronger than the value of Order1
.
Uses a specified value to replace the stored value of *this
.
Ty atomic<Ty>::exchange(
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::exchange(
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
The stored value of *this
before the exchange.
This operation performs a read-modify-write operation to use Value
to replace the value that is stored in *this
, within the memory constraints that are specified by Order
.
Fetches the value stored in *this
, and then adds a specified value to the stored value.
Ty atomic<Ty>::fetch_add (
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::fetch_add (
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
A Ty
object that contains the value stored in *this
prior to the addition.
The fetch_add
method performs a read-modify-write operation to atomically add Value
to the stored value in *this
, and applies the memory constraints that are specified by Order
.
Performs a bitwise "and" (&
) on a value and an existing value that is stored in *this
.
Ty atomic<Ty>::fetch_and (
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::fetch_and (
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
A Ty
object that contains the result of the bitwise "and" (&
).
The fetch_and
method performs a read-modify-write operation to replace the stored value of *this
with a bitwise "and" (&
) of Value
and the current value that is stored in *this
, within the memory constraints that are specified by Order
.
Performs a bitwise "or" (|
) on a value and an existing value that is stored in *this
.
Ty atomic<Ty>::fetch_or (
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::fetch_or (
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
A Ty
object that contains the result of the bitwise "or" (|
).
The fetch_or
method performs a read-modify-write operation to replace the stored value of *this
with a bitwise "or" (|
) of Value
and the current value that is stored in *this
, within the memory constraints that are specified by Order
.
Subtracts a specified value from the stored value.
Ty atomic<Ty>::fetch_sub (
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::fetch_sub (
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
A Ty
object that contains the result of the subtraction.
The fetch_sub
method performs a read-modify-write operation to atomically subtract Value
from the stored value in *this
, within the memory constraints that are specified by Order
.
Performs a bitwise "exclusive or" (^
) on a value and an existing value that is stored in *this
.
Ty atomic<Ty>::fetch_xor (
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
Ty atomic<Ty>::fetch_xor (
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A value of type Ty
.
Order
A memory_order
.
A Ty
object that contains the result of the bitwise "exclusive or" (^
).
The fetch_xor
method performs a read-modify-write operation to replace the stored value of *this
with a bitwise "exclusive or" (^
) of Value
and the current value that is stored in *this
, and applies the memory constraints that are specified by Order
.
Specifies whether atomic
operations on *this
are lock free.
bool is_lock_free() const volatile noexcept;
true
if atomic
operations on *this
are lock free; otherwise, false
.
An atomic
type is lock free if no atomic
operations on that type use locks.
Retrieves the stored value in *this
, within the specified memory constraints.
Ty atomic::load(
memory_order Order = memory_order_seq_cst
) const volatile noexcept;
Ty atomic::load(
memory_order Order = memory_order_seq_cst
) const noexcept;
Order
A memory_order
. Order
must not be memory_order_release
or memory_order_acq_rel
.
The retrieved value that is stored in *this
.
Stores a specified value.
void atomic<Ty>::store(
Ty Value,
memory_order Order = memory_order_seq_cst
) volatile noexcept;
void atomic<Ty>::store(
Ty Value,
memory_order Order = memory_order_seq_cst
) noexcept;
Value
A Ty
object.
Order
A memory_order
constraint.
This member function atomically stores Value
in *this
, within the memory constraints that are specified by Order
.