Atomic Operation
An atomic operation executes indivisibly, visible to concurrent observers and not interruptible, achieving simple concurrency safety without locks.
Purpose
A lock-free concurrency primitive, lighter than locks, used to implement counters and flags.
Mechanism
Hardware provides the CAS (compare-and-swap) primitive: an atomic compare-and-exchange. Atomic increment/decrement/flag operations follow. Memory ordering controls visibility (acquire/release/seq_cst). Cheaper than locks but only suitable for simple operations.
Use cases
- Lock-free concurrency
- Counters
- CAS