How it works...

std::atomic is a class template that defines (including its specializations) an atomic type. The behavior of the objects of atomic types is well defined when one thread writes to the object and the other reads data, without using locks to protect access. The std::atomic class provides several specializations:

  • Full specialization for bool, with a typedef called atomic_bool.
  • Full specialization for all integral types, with typedefs called atomic_int, atomic_long, atomic_char, atomic_wchar, and many others.
  • Partial specialization for pointer types.

The atomic class template has various member functions that perform atomic operations such as the following:

  • load() to atomically load and return the value of the object.
  • store() ...

Get Modern C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.