← Lessons

quiz vs the machine

Platinum1790

Concurrency

The Relaxed Atomics Deep Dive

Atomicity without ordering, and the narrow cases where it is correct.

6 min read · advanced · beat Platinum to climb

Atomicity only

A relaxed atomic operation guarantees only that the access itself is atomic, meaning indivisible and free of torn reads or writes. It provides no ordering with respect to other memory operations and creates no happens before edges with other threads.

What relaxed still guarantees

Relaxed is not anarchy. Some properties survive even without ordering.

  • Each atomic variable has a single modification order, and a thread that reads it sees values consistent with that order, never going backward.
  • Read modify write operations like fetch add remain atomic.
  • But independent variables can appear reordered relative to each other across threads.

Where it is appropriate

The classic safe use is a counter where only the final total matters, such as gathering statistics or a reference count increment. Incrementing with relaxed is correct because no other data depends on the counter's order. A reference count decrement that may free an object, however, needs release acquire ordering, since freeing must not be reordered before the last use.

Key idea

Relaxed atomics give indivisibility and a per variable modification order but no cross variable ordering, so use them only when no other data depends on the operation's order.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a relaxed atomic guarantee?

2. Why is a relaxed increment fine for a statistics counter?