liveBook · Manning

Atomic type

As explained in C++ Concurrency in Action, Second Edition

An atomic type is a type whose operations provide atomic access to an object and support synchronization between threads.

An atomic type makes its operations atomic in the C++ language sense and provides building blocks for synchronization between threads. These low-level facilities matter most when working close to the machine; mutexes and other higher-level tools can often avoid the need to manage memory-order details directly. section 5.0 section 5.2.1

Keep these practical limits in mind:

  • Standard atomic types are declared in <atomic>. Check is_lock_free() to learn whether an operation uses atomic instructions or an internal lock; if it uses a lock, a mutex-based implementation may be easier to get right. section 5.2.1
  • Atomic operations do not make unsynchronized accesses to other non-atomic data safe. A data race on non-atomic variables can cause undefined behavior. section 5.2
Table 5.1. The alternative names for the standard atomic types and their corresponding std::atomic<> specializations (view table figure)

Atomic type

Corresponding specialization

atomic_bool std::atomic<bool>
atomic_char std::atomic<char>
atomic_schar std::atomic<signed char>
atomic_uchar std::atomic<unsigned char>
atomic_int std::atomic<int>
atomic_uint std::atomic<unsigned>
atomic_short std::atomic<short>
atomic_ushort std::atomic<unsigned short>
atomic_long std::atomic<long>
atomic_ulong std::atomic<unsigned long>
atomic_llong std::atomic<long long>
atomic_ullong std::atomic<unsigned long long>
atomic_char16_t std::atomic<char16_t>
atomic_char32_t std::atomic<char32_t>
atomic_wchar_t std::atomic<wchar_t>

Use the table to look up standard atomic type names and their corresponding std::atomic specializations. section 5.2.1

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Descends from atomic types into operations, memory ordering, and fences, including ordering between non-atomic operations.
Examples
Names standard types such as std::atomic<int> and contrasts lock-free operations with implementations using an internal lock.
What you can do after
Evaluate lock-free behavior with is_lock_free() and weigh atomic operations against a mutex-based implementation.
How it connects
Links atomic types to the C++ memory model, synchronization, mutexes, and memory fences.

Also explained in

Compare how 17 books explain atomic type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.