In order to use the native C++11 atomics features, all one has to do is include the <atomic> header. This makes available the atomic class, which uses templates to adapt itself to the required type, with a large number of predefined typedefs:
|
Typedef name |
Full specialization |
|
std::atomic_bool |
std::atomic<bool> |
|
std::atomic_char |
std::atomic<char> |
|
std::atomic_schar |
std::atomic<signed char> |
|
std::atomic_uchar |
std::atomic<unsigned char> |
|
std::atomic_short |
std::atomic<short> |
|
std::atomic_ushort |
std::atomic<unsigned short> |
|
std::atomic_int |
std::atomic<int> |
|
std::atomic_uint |
std::atomic<unsigned int> |
|
std::atomic_long |
std::atomic<long> |
|
std::atomic_ulong |
std::atomic<unsigned long> |
|
std::atomic_llong |
std::atomic<long long> |
|
std::atomic_ullong |
std::atomic<unsigned long long> |
|
std::atomic_char16_t |
std::atomic<char16_t> |
|
std::atomic_char32_t |
std::atomic<char32_t> |
|
std::atomic_wchar_t |
std::atomic<wchar_t> |
|
std::atomic_int8_t |
std::atomic<std::int8_t> |
|
std::atomic_uint8_t |
std::atomic<std::uint8_t> |
|
std::atomic_int16_t |
std::atomic<std::int16_t> |
|
std::atomic_uint16_t |
std::atomic<std::uint16_t> |
|
std::atomic_int32_t |
std::atomic<std::int32_t> |
|
std::atomic_uint32_t |
std::atomic<std::uint32_t> |
|
std::atomic_int64_t |
std::atomic<std::int64_t> |
|
std::atomic_uint64_t |
std::atomic<std::uint64_t> |
|
std::atomic_int_least8_t |
std::atomic<std::int_least8_t> |
|
std::atomic_uint_least8_t |
std::atomic<std::uint_least8_t> |
|
std::atomic_int_least16_t |
std::atomic<std::int_least16_t> |
|
std::atomic_uint_least16_t |
std::atomic<std::uint_least16_t> |
|
std::atomic_int_least32_t |
std::atomic<std::int_least32_t> |
|
std::atomic_uint_least32_t |
std::atomic<std::uint_least32_t> |
|
std::atomic_int_least64_t |
std::atomic<std::int_least64_t> |
|
std::atomic_uint_least64_t |
std::atomic<std::uint_least64_t> |
|
std::atomic_int_fast8_t |
std::atomic<std::int_fast8_t> |
|
std::atomic_uint_fast8_t |
std::atomic<std::uint_fast8_t> |
|
std::atomic_int_fast16_t |
std::atomic<std::int_fast16_t> |
|
std::atomic_uint_fast16_t |
std::atomic<std::uint_fast16_t> |
|
std::atomic_int_fast32_t |
std::atomic<std::int_fast32_t> |
|
std::atomic_uint_fast32_t |
std::atomic<std::uint_fast32_t> |
|
std::atomic_int_fast64_t |
std::atomic<std::int_fast64_t> |
|
std::atomic_uint_fast64_t |
std::atomic<std::uint_fast64_t> |
|
std::atomic_intptr_t |
std::atomic<std::intptr_t> |
|
std::atomic_uintptr_t |
std::atomic<std::uintptr_t> |
|
std::atomic_size_t |
std::atomic<std::size_t> |
|
std::atomic_ptrdiff_t |
std::atomic<std::ptrdiff_t> |
|
std::atomic_intmax_t |
std::atomic<std::intmax_t> |
|
std::atomic_uintmax_t |
std::atomic<std::uintmax_t> |
This atomic class defines the following generic functions:
|
Function |
Description |
|
operator= |
Assigns a value to an atomic object. |
|
is_lock_free |
Returns true if the atomic object is lock-free. |
|
store |
Replaces the value of the atomic object with a non-atomic argument, atomically. |
|
load |
Atomically obtains the value of the atomic object. |
|
operator T |
Loads a value from an atomic object. |
|
exchange |
Atomically replaces the value of the object with the new value and returns the old value. |
|
compare_exchange_weak compare_exchange_strong |
Atomically compares the value of the object and swaps values if equal, or else returns the current value. |
With the C++17 update, the is_always_lock_free constant is added. This allows one to inquire whether the type is always lock-free.
Finally, we have the specialized atomic functions:
|
Function |
Description |
|
fetch_add |
Atomically adds the argument to the value stored in the atomic object and returns the old value. |
|
fetch_sub |
Atomically subtracts the argument from the value stored in the atomic object and returns the old value. |
|
fetch_and |
Atomically performs bitwise AND between the argument and the value of the atomic object and returns the old value. |
|
fetch_or |
Atomically performs bitwise OR between the argument and the value of the atomic object and returns the old value. |
|
fetch_xor |
Atomically performs bitwise XOR between the argument and the value of the atomic object and returns the old value. |
|
operator++ operator++(int) operator-- operator--(int) |
Increments or decrements the atomic value by one. |
|
operator+= operator-= operator&= operator|= operator^= |
Adds, subtracts, or performs a bitwise AND, OR, XOR operation with the atomic value. |