liveBook · Manning

Atomic operation

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

An atomic operation is an operation that appears indivisible to other threads under the C++ memory model.

An atomic operation is indivisible from the perspective of other threads: they cannot observe it half-complete. It is either done or not done. In C++, atomic types provide atomic operations in most cases. section 5.2 section 5.2

Atomicity matters when threads access shared objects:

  • If a load and all modifications to an object are atomic, the load reads the initial value or a value stored by one of those modifications. section 5.2
  • Non-atomic operations can expose partially completed changes. Unsynchronized access to non-atomic variables can cause a data race and undefined behavior. section 5.2
  • Atomic operations provide low-level synchronization with defined semantics and can support ordering between operations on separate threads. section 1.3.3 section 5.4

Atomicity does not make a multi-part non-atomic operation indivisible: other threads may observe only some constituent atomic operations. Atomic operations are a basis for synchronization, but their memory-ordering details are complex. section 5.2 section 5.0

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

How deep it goes
Defines indivisibility and its consequences, then places atomic operations within synchronization and memory ordering.
What it assumes
Builds from memory-model basics before treating atomic types and operations.
What you can do after
Understand how atomic operations prevent partial observation and contribute to synchronization between threads.
How it connects
Connects atomic operations to atomic types, the C++ memory model, synchronization, fences, and data races.

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.