Concurrency in Depth · 21 min · free with an account

Atomics and Ordering

Lesson preview · about 30%

An atomic is a value several threads may touch without a lock.

Core idea

Atomicity and ordering are separate promises; Relaxed gives only the first.

Start with the idea

Ordering is the interesting half of the answer. Relaxed guarantees only that the operation itself is indivisible; Acquire and Release pair up to publish everything written before a release to whoever performs the matching acquire; SeqCst adds a single global order and costs the most. SeqCst provides a stronger model, but it does not repair a flawed protocol and its cost depends on the architecture and operation.

An atomic operation is indivisible with respect to competing atomic accesses, but a sequence of two atomics is not automatically a transaction. For a shared event counter whose final value is read after all workers join, fetch_add with Relaxed ordering may be enough: the count is accurate, and the join supplies the completion boundary.

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.

← All lessons in Rust Interview Prep