expression
Explain a lost update caused by preemption
A read-modify-write sequence can be interrupted between the read and write. The following explicit sequence models two increments where one is lost. No actual interrupt, data race or unsafe shared memory occurs. Correct synchronization depends on the processor and every competing execution context. Expected host stdout (not executed): interrupt result: 1 final: 1, expected two events Common mistake: Assuming volatile or a single source-level += expression prevents lost updates.
Formal Syntax & Structure
rust syntaxrust
read → interrupt update → write stale valueWorking Example
Example Snippetrust
fn main() { let mut count = 0u32; let snapshot = count; count += 1; // Model the interrupt's increment. println!("interrupt result: {count}"); count = snapshot + 1; // Model resumed main code using stale data. println!("final: {count}, expected two events");}