expression
Loom model: paired fields under one lock
A modeled Mutex protects two related fields. The writer increments both fields inside one critical section; the reader checks equality while holding the same lock. Check the relationship, not an arbitrary scheduling order. Staged Cargo test, not standalone Rust or a ByteMind exercise. Expected test outcome (not executed): PASS Common mistake: Using separate lock acquisitions to check a relationship that may change between them.
Formal Syntax & Structure
rust syntaxrust
loom::model(|| { /* modeled operations */ });Working Example
Example Snippetrust
#[test]fn loom_mutex_invariant() { loom::model(|| { use loom::{sync::{Arc, Mutex}, thread}; let state = Arc::new(Mutex::new((0u32, 0u32))); let w = state.clone(); let t = thread::spawn(move || { let mut pair = w.lock().unwrap(); pair.0 += 1; pair.1 += 1; }); { let pair = state.lock().unwrap(); assert_eq!(pair.0, pair.1); } t.join().unwrap(); assert_eq!(*state.lock().unwrap(), (1, 1)); });}