Relaxed ordering
Quick answer
Relaxed preserves atomicity and the modification order of an individual atomic object without establishing synchronization for other data.
Use relaxed counters narrowly
For independent statistics, a relaxed read-modify-write can be sufficient. Joining all workers provides the completion synchronization for a final total. A relaxed flag alone does not publish surrounding memory. Live readings may change while workers are active; this is not a consistent snapshot of multiple counters.
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};fn main() { let jobs = AtomicUsize::new(0); std::thread::scope(|s| { for _ in 0..3 { s.spawn(|| { jobs.fetch_add(2, Relaxed); }); } }); println!("{}", jobs.load(Relaxed));}Expected stdout (not executed):
6Scope completion, not Relaxed alone, establishes that workers finished.
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};fn main() { let hits = AtomicUsize::new(0); let misses = AtomicUsize::new(0); hits.fetch_add(3, Relaxed); misses.fetch_add(1, Relaxed); println!("{} {}", hits.load(Relaxed), misses.load(Relaxed));}Expected stdout (not executed):
3 1This single-thread example illustrates separate counters, not a multi-counter snapshot guarantee.
Common mistake
Do not choose Relaxed for a readiness flag just because the flag itself is atomic.
Try it yourself
Explain why joining matters in the first example.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Relaxed ordering — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A relaxed atomic counts completed independent jobs. When is a final total suitable to inspect?
Relaxed ordering — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; fn main() { let jobs = AtomicUsize::new(0); std::thread::scope(|s| { for _ in 0..3 { s.spawn(|| { jobs.fetch_add(2, Relaxed); }); } }); println!("{}", jobs.load(Relaxed)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Relaxed ordering — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; fn main() { let hits = AtomicUsize::new(0); let misses = AtomicUsize::new(0); hits.fetch_add(3, Relaxed); misses.fetch_add(1, Relaxed); println!("{} {}", hits.load(Relaxed), misses.load(Relaxed)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.