
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 |