1 个不稳定版本
0.1.0 | 2021年2月26日 |
---|
#18 在 #triple
10KB
177 行
示例
#[derive(Clone)]
struct State {
v: u64,
}
use std::time::Duration;
use simple_triple_buffer::new_with;
fn main() {
let (mut w, mut r) = new_with(State { v: 0 }, |s| {
println!("Cloned state!");
s.clone()
});
let tw = std::thread::spawn(move || loop {
w.write_new(|last, new| {
new.v = last.v + 1;
});
});
let tr = std::thread::spawn(move || {
let mut last = 0;
loop {
let state = r.read_newest();
println!("Value: {} (+{})", state.v, state.v - last);
last = state.v;
std::thread::sleep(Duration::from_millis(20));
}
});
tw.join().unwrap();
tr.join().unwrap();
}