C++ Library - <atomic>
Introduction
It is an objects of atomic types contain a value of a particular type (T) and the main characteristic of atomic objects is that access to this contained value from different threads cannot cause data races (i.e., doing that is well-defined behavior, with accesses properly sequenced). Generally, for all other objects, the possibility of causing a data race for accessing the same object concurrently qualifies the operation as undefined behavior.
Declaration
Following is the declaration for std::atomic.
template <class T> struct atomic;
Parameters
T − It is a type of the contained value.
Member functions
| Sr.No. | Member functions & Definition |
|---|---|
| 1 |
(constructor)
It is a constructs an atomic object |
| 2 |
operator=
It is stores a value into an atomic object |
| 3 |
is_lock_free
It checks if the atomic object is lock-free |
| 4 |
store
It atomically replaces the value of the atomic object with a non-atomic argument |
| 5 |
load
It atomically obtains the value of the atomic object |
| 6 |
operator T
It loads a value from an atomic object |
| 7 |
exchange
It atomically replaces the value of the atomic object and obtains the value held previously |
| 8 |
compare_exchange_weak & compare_exchange_strong
It atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not |
Operations supported by certain specializations
| Sr.No. | Operations & Definition |
|---|---|
| 1 |
fetch_add
It atomically adds the argument to the value stored in the atomic object and obtains the value held previously |
| 2 |
fetch_sub
It atomically subtracts the argument from the value stored in the atomic object and obtains the value held previously |
| 3 |
fetch_and
It atomically performs bitwise AND between the argument and the value of the atomic object and obtains the value held previously |
| 4 |
fetch_or
It atomically performs bitwise OR between the argument and the value of the atomic object and obtains the value held previously |
| 5 |
fetch_xor
It atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not |
| 6 |
operator++
It increments the atomic value by one |
| 7 |
operator--
It decrements the atomic value by one |
atomic.htm