
Understand it
Atomicity and ordering answer different questions. Relaxed preserves the atomic operation without providing publication of unrelated data. Acquire/Release can establish a synchronization relationship when the relevant read observes the release; SeqCst adds ordering guarantees but does not repair an invalid protocol or have one universal performance cost.
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. If an atomic flag publishes ordinary data written by one thread, the flag also needs appropriate synchronization so another thread sees the data after observing the flag. A release store paired with an acquire load can provide that relationship.
Sequential consistency gives a stronger global ordering model for sequentially consistent atomic operations. It is easier to reason about in many cases, but it is not a cure for an incorrect protocol, and its performance cost varies by architecture and operation. Do not claim it is always slow or always sufficient. The ordering rule should follow the invariant: what data must a reader see, and after which observation?
For an interview, draw producer and consumer timelines. Mark the data write, the flag store, the flag load, and the data read. Explain why seeing the flag is meaningful only with the right synchronization. Then contrast it with a simple counter that needs no publication of other data. If the proof becomes hard to communicate, a mutex may be the better engineering choice. Correctness and maintainability outrank a speculative micro-optimization.
If the interviewer asks about Relaxed, say what it deliberately does not promise: visibility ordering for unrelated ordinary writes.
Interview follow-up: Change the counter into a ready flag publishing a payload. Which acquire must observe which release, and what protects the payload’s accesses?
Read the code
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNT: AtomicUsize = AtomicUsize::new(0);
fn main() {
let workers: Vec<_> = (0..4).map(|_| std::thread::spawn(|| {
COUNT.fetch_add(1, Ordering::Relaxed);
})).collect();
for worker in workers { worker.join().unwrap(); }
assert_eq!(COUNT.load(Ordering::Relaxed), 4);
}Desktop OS-thread example: atomic increments and joins; this offline WASI runtime does not supply OS threads.
Try it
Explain Relaxed, Acquire/Release, and SeqCst in one minute each.
What success looks like: You can say what each ordering guarantees and what it costs.
Check your understanding
On a desktop with OS threads, when is Relaxed suitable for this counter after all workers have joined?
Reveal the answer
- Relaxed also publishes every unrelated non-atomic write to every reader.
- Only the counter value is needed; thread completion supplies the separate join boundary.
- Relaxed becomes sufficient only when there are no concurrent increments.
Atomic increments avoid lost updates; join establishes worker completion. A flag publishing other data needs a separate synchronization argument.
Go deeper
Common misconception: Relaxed becomes sufficient only when there are no concurrent increments.
Relaxed increments are still atomic, so concurrent fetch_add calls lose no updates. What Relaxed omits is ordering with other data, which this counter does not need.
- Identify the contract: An atomic is a value several threads may touch without a lock.
- Trace the concrete example: Four
fetch_addcalls lose no updates, and eachjoincompletes before the final load, so it reads 4. Nothing here uses Release or Acquire. - Test the changed constraint: Change the counter into a ready flag publishing a payload. Which acquire must observe which release, and what protects the payload’s accesses?
Transfer challenge: Change the counter into a ready flag publishing a payload. Which acquire must observe which release, and what protects the payload’s accesses?
Connections
- Reasoning About unsafe: Sound abstractions and invariants
- Deadlocks: What the borrow checker does not prevent
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Atomics and Ordering (courseId "interview", lessonId "q-atomics") Link: https://crabrix.com/learn/interview/q-atomics/ Goal: Atomicity and ordering are separate promises; Relaxed gives only the first. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-atomics) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.