expression
Explain why two separate accesses do not make an atomic update
A read-modify-write operation performed as separate loads and stores can lose an update when another execution context intervenes. Volatile does not make this sequence indivisible. This safe sequential model demonstrates the lost-update schedule without executing a data race or pretending to manipulate device registers. Expected host stdout (not executed): first: 1 final: 1, expected updates: 2 Common mistake: Assuming two volatile accesses make an atomic read-modify-write or applying CPU atomics to arbitrary MMIO.
Formal Syntax & Structure
rust syntaxrust
read; compute; write vs atomic fetch_addWorking Example
Example Snippetrust
fn main() { let mut counter = 0u32; let foreground = counter; let interrupt = counter; counter = foreground + 1; println!("first: {counter}"); counter = interrupt + 1; println!("final: {counter}, expected updates: 2");}