race/
race.rs

1use std::sync::Arc;
2use std::thread;
3
4use unlock::RwLock;
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let lock = Arc::new(RwLock::new(0u64));
8
9    let mut threads = Vec::new();
10
11    unlock::capture();
12
13    for _ in 0..100 {
14        let lock = lock.clone();
15
16        threads.push(thread::spawn(move || {
17            let mut sum = 0u64;
18
19            for n in 0..100 {
20                if n % 4 == 0 {
21                    *lock.write() += 1;
22                } else {
23                    sum += *lock.read();
24                }
25
26                std::thread::sleep(std::time::Duration::from_millis(n % 11));
27            }
28
29            sum
30        }));
31    }
32
33    let mut total = 0;
34
35    for thread in threads {
36        total += thread.join().unwrap();
37    }
38
39    dbg!(total);
40
41    let events = unlock::drain();
42    dbg!(events.len());
43    unlock::html::write("trace.html", &events)?;
44    Ok(())
45}