try_mutex/
try-mutex.rs

1use std::{hint::spin_loop, sync::Arc, thread};
2
3use option_lock::Mutex;
4
5// FIXME - this example would work equally well with a simple atomic
6
7fn main() {
8    let shared = Arc::new(Mutex::new(0i32));
9    let threads = 100;
10    for _ in 0..threads {
11        let shared = shared.clone();
12        thread::spawn(move || {
13            let mut guard = shared.spin_lock().unwrap();
14            *guard += 1;
15        });
16    }
17    loop {
18        if shared.try_copy() == Ok(threads) {
19            break;
20        }
21        spin_loop()
22    }
23    println!("Completed {} threads", threads);
24}