expression

Loom counterexample: load/store lost update

An intentionally failing test: two workers separately read a counter and then store an incremented value. The invariant of two increments is violated in an interleaving where both load the initial value. Run this negative fixture separately; a failure from the marked assertion is the expected evidence. Staged Cargo test, not standalone Rust or a ByteMind exercise. Expected test outcome (not executed): FAIL (intentional assertion violation) Common mistake: Calling any compile error or timeout a successful counterexample.

Formal Syntax & Structure

rust syntaxrust
loom::model(|| { /* modeled operations */ });

Working Example

Example Snippetrust
#[test]fn loom_lost_update_counterexample() {    loom::model(|| {        use loom::{sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}}, thread};        let n = Arc::new(AtomicUsize::new(0));        let a = n.clone(); let b = n.clone();        let t1 = thread::spawn(move || { let v = a.load(Relaxed); a.store(v + 1, Relaxed); });        let t2 = thread::spawn(move || { let v = b.load(Relaxed); b.store(v + 1, Relaxed); });        t1.join().unwrap(); t2.join().unwrap();        assert_eq!(n.load(Relaxed), 2, "lost update invariant");    });}
Rust Reference