std-function
Atomic fence: synchronization with an atomic carrier
Fences can participate in release/acquire synchronization when paired with appropriate atomic reads and writes. A fence alone is not inter-thread communication. The example conditionally observes a release store before executing an acquire fence. Expected stdout (not executed): 7 Common mistake: Using fences around non-atomic shared writes without an atomic synchronization carrier.
Formal Syntax & Structure
rust syntaxrust
std::sync::atomic::fence(order: Ordering)Working Example
Example Snippetrust
fn main() { use std::{sync::atomic::{AtomicBool, AtomicUsize, Ordering::*, fence}, thread}; let flag = AtomicBool::new(false); let data = AtomicUsize::new(0); thread::scope(|s| { s.spawn(|| { data.store(7, Relaxed); flag.store(true, Release); }); if flag.load(Relaxed) { fence(Acquire); assert_eq!(data.load(Relaxed), 7); } }); println!("{}", data.load(Relaxed));}