Start with the idea
Atomic operations are indivisible but still need an ordering that describes cross-thread visibility. Use the weakest ordering justified by a written synchronization argument, not by habit.
An atomic value supports operations that are indivisible with respect to competing threads. fetch_add on AtomicUsize, for example, prevents two workers from losing an increment by reading and writing the same stale count. This makes atomics a good fit for simple counters, flags, and some carefully designed state machines. It does not turn a sequence of several atomic operations into one transaction. If two fields must change together, a mutex may express the invariant more clearly.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.