simple-triple-buffer

Crates.iosimple-triple-buffer
lib.rssimple-triple-buffer
version0.1.0
sourcesrc
created_at2021-02-26 00:52:37.643331
updated_at2021-02-26 00:52:37.643331
descriptionA simple triple buffer.
homepage
repositoryhttps://github.com/Kimundi/simple_triple_buffer
max_upload_size
id360759
size22,345
Marvin Löbel (Kimundi)

documentation

https://docs.rs/simple_triple_buffer

README

Example

#[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();
}
Commit count: 12

cargo fmt