Using atomic types
The thread support library offers functionalities for managing threads and synchronizing access to shared data with mutexes and locks, and, as of C++20, with latches, barriers, and semaphores. The standard library provides support for the complementary, lower-level atomic operations on data, which are indivisible operations that can be executed concurrently from different threads on shared data, without the risk of producing race conditions and without the use of locks. The support it provides includes atomic types, atomic operations, and memory synchronization ordering. In this recipe, we will see how to use some of these types and functions.
All the atomic types and operations are defined in the
std namespace in the
How to do it...
The following are a series of typical operations that use atomic types:
- Use the
std::atomicclass template to create atomic objects that support atomic operations...