-
Book Overview & Buying
-
Table Of Contents
C++ STL Cookbook - Second Edition
By :
The std::atomic class encapsulates a single object and guarantees it to be atomic, such that concurrent access to that object does not cause a race condition. Memory order semantics may also be specified, controlling how operations on different threads are ordered and made visible. It is typically used for low-level synchronization and lock-free communication among threads.
std::atomic defines an atomic type from its template type. The type must be trivial. A type is trivial if it occupies contiguous memory, has no user-defined constructor, and has no virtual member functions. All primitive types are trivial.
While it is possible to construct a trivial type, std::atomic is most often used with simple primitives, such as bool, int, long, float, and double.
This recipe uses a simple function that loops over a counter to demonstrate sharing atomic objects. We will spawn a swarm of these loops as threads that share atomic...