expression

Loom model: joined atomic increments

Model two workers sharing a counter with Loom atomics, Arc and threads. Assert the exact total after both joins. This is a small modeled invariant, not an exhaustive proof of a larger library. Staged Cargo test, not standalone Rust or a ByteMind exercise. Expected test outcome (not executed): PASS Common mistake: Using std atomics inside the model where interleavings need to be modeled.

Formal Syntax & Structure

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

Working Example

Example Snippetrust
#[test]fn loom_joined_counter() {    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 || { a.fetch_add(1, Relaxed); });        let t2 = thread::spawn(move || { b.fetch_add(1, Relaxed); });        t1.join().unwrap(); t2.join().unwrap();        assert_eq!(n.load(Relaxed), 2);    });}
Rust Reference