C++ Library - <atomic>



Introduction

These are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads and he atomic library provides components for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object.

Atomic type

Sr.No.Member types & Definition
1atomic

Atomic class template and specializations for bool, integral, and pointer types

C-style atomic types

The following are atomic types and also defined in this header.

contained typeatomic typedescription
boolatomic_bool
charatomic_char

atomics for fundamental integral types.

These are either typedefs of the corresponding full specialization of the atomic class template or a base class of such specialization.

signed charatomic_schar
unsigned charatomic_uchar
shortatomic_short
unsigned shortatomic_ushort
intatomic_int
unsigned intatomic_uint
longatomic_long
unsigned longatomic_ulong
long longatomic_llong
unsigned long longatomic_ullong
wchar_tatomic_wchar_t
char16_tatomic_char16_t
char32_tatomic_char32_t
intmax_tatomic_intmax_t

atomics for width-based integrals (those defined in <cinttypes>).

Each of these is either an alias of one of the above atomics for fundamental integral types or of a full specialization of the atomic class template with an extended integral type.

Where N is one in 8, 16, 32, 64, or any other type width supported by the library.

uintmax_tatomic_uintmax_t
int_leastN_tatomic_int_leastN_t
uint_leastN_tatomic_uint_leastN_t
int_fastN_tatomic_int_fastN_t
uint_fastN_tatomic_uint_fastN_t
intptr_tatomic_intptr_t
uintptr_tatomic_uintptr_t
size_tatomic_size_t
ptrdiff_tatomic_ptrdiff_t

Operations on atomic types

&
Sr.No.Operations & Definition
1atomic_is_lock_free

It is used to checks if the atomic type's operations are lock-free

2atomic_store & atomic_store_explicit

It automically replaces the value of the atomic object with a non-atomic argument

3atomic_load & atomic_load_explicit

It atomically obtains the value stored in an atomic object

4atomic_exchange & atomic_exchange_explicit

It atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic

5atomic_compare_exchange_weak & atomic_compare_exchange_weak_explicit & atomic_compare_exchange_strong & atomic_compare_exchange_strong_explicit

It atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not

6atomic_fetch_add & atomic_fetch_add_explicit

It adds a non-atomic value to an atomic object and obtains the previous value of the atomic

7atomic_fetch_sub & atomic_fetch_sub_explicit

It subtracts a non-atomic value from an atomic object and obtains the previous value of the atomic

8atomic_fetch_and & atomic_fetch_and_explicit

It replaces the atomic object with the result of logical AND with a non-atomic argument and obtains the previous value of the atomic

9atomic_fetch_or & atomic_fetch_or_explicit

It replaces the atomic object with the result of logical OR with a non-atomic argument and obtains the previous value of the atomic

10atomic_fetch_xor & atomic_fetch_xor_explicit

It replaces the atomic object with the result of logical XOR with a non-atomic argument and obtains the previous value of the atomic

Flag type and operations

Sr.No.Flag type & Definition
1atomic_flag

The lock-free boolean atomic type

2atomic_flag_test_and_set & atomic_flag_test_and_set_explicit

It atomically sets the flag to true and returns its previous value

3atomic_flag_clear & atomic_flag_clear_explicit

It atomically sets the value of the flag to false