Acquire and release
Quick answer
Release and Acquire can synchronize publication of data: when an acquire load observes a release store on the same atomic, earlier operations of the publishing thread become ordered before later operations of the reader.
Publish then observe
Use a release store after preparing data and an acquire load before consuming it. The synchronization depends on what the acquire reads, not simply on using the words Acquire and Release. Diagram: writer payload.store → writer ready.store(Release) → reader ready.load(Acquire, observes true) → reader payload.load. The middle arrow is the synchronizes-with edge. The following one-shot protocol has one writer, no reset, and an atomic payload. The read-modify-write variant AcqRel combines acquire and release effects.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::*};fn main() { let data = AtomicUsize::new(0); let ready = AtomicBool::new(false); std::thread::scope(|s| { s.spawn(|| { data.store(42, Relaxed); ready.store(true, Release); }); if ready.load(Acquire) { assert_eq!(data.load(Relaxed), 42); } }); assert!(ready.load(Acquire)); println!("{}", data.load(Relaxed));}Expected stdout (not executed):
42The conditional assertion checks the observation when it occurs; the final join also synchronizes.
use std::sync::atomic::{AtomicBool, Ordering::AcqRel};fn main() { let busy = AtomicBool::new(false); println!("{}", busy.swap(true, AcqRel));}Expected stdout (not executed):
falseThe exchange returns the old flag; this alone is not a complete lock protocol.
Common mistake
Do not use Release on an atomic load or Acquire on a plain atomic store.
Try it yourself
Draw data.store → ready.store → ready.load → data.load and label the conditional synchronization edge.
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
Acquire and release — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A producer writes data then publishes a flag. Which load/store pairing can establish publication synchronization?
Acquire and release — 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::{AtomicBool, AtomicUsize, Ordering::*}; fn main() { let data = AtomicUsize::new(0); let ready = AtomicBool::new(false); std::thread::scope(|s| { s.spawn(|| { data.store(42, Relaxed); ready.store(true, Release); }); if ready.load(Acquire) { assert_eq!(data.load(Relaxed), 42); } }); assert!(ready.load(Acquire)); println!("{}", data.load(Relaxed)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Acquire and release — 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::{AtomicBool, Ordering::AcqRel}; fn main() { let busy = AtomicBool::new(false); println!("{}", busy.swap(true, AcqRel)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.