Skip to main content

example/
example.rs

1use std::{
2    thread::sleep,
3    time::{Duration, Instant},
4};
5fn main() {
6    let x = rcu_128::RcuCell::new(0);
7    std::thread::scope(|s| {
8        s.spawn(|| {
9            for i in 0..40 {
10                let t = Instant::now();
11                if i < 20 {
12                    x.write(i);
13                } else {
14                    x.update(|v| v + 1);
15                }
16                println!("Update {i} used time: {:?}", t.elapsed());
17                sleep((t + Duration::from_millis(100)).duration_since(Instant::now()));
18            }
19        });
20        s.spawn(|| {
21            // Always has 4 guards alive
22            let mut guards = [x.read(), x.read(), x.read(), x.read()];
23            for idx in 0..400 {
24                let r = x.read();
25                println!("Read value: {}", *r);
26                guards[idx % 4] = r;
27                sleep(Duration::from_millis(10));
28            }
29        });
30    })
31}