atb

Crates.ioatb
lib.rsatb
version0.1.0
sourcesrc
created_at2022-10-08 01:26:41.580414
updated_at2022-10-08 01:26:41.580414
descriptionSimple lock-free triple buffer
homepagehttps://github.com/jf2048/atb
repositoryhttps://github.com/jf2048/atb.git
max_upload_size
id683234
size15,999
Jason Francis (jf2048)

documentation

https://docs.rs/atb

README

atb

A simple lock-free SPSC triple buffering implementation for Rust.

This data structure is optimized for situations where a producer thread needs to rebuild a larger data set periodically and send it to a real-time consumer thread. The third buffer is always available to the producer thread, so it never needs to wait to start producing a new frame.

Example

const W: usize = 320;
const H: usize = 240;

let pixels = Arc::new(AtomicTripleBuffer::new([0u32; W * H]));

{
    let pixels = pixels.clone();
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(Duration::from_secs_f64(1.0 / 60.0));
            let front = pixels.front_buffer().unwrap();
            // ... display `front` on the screen ...
        }
    });
}

let mut counter = 0u8;
loop {
    let mut bufs = pixels.back_buffers().unwrap();
    let back = bufs.back_mut();
    for y in 0..H {
        let c = counter.wrapping_add(y as u8) as u32;
        let c = c | (c << 8) | (c << 16) | (c << 24);
        for x in 0..W {
            back[y * W + x] = c;
        }
    }
    counter = counter.wrapping_add(1);
    bufs.swap();
    std::thread::sleep(Duration::from_secs_f64(1.0 / 24.0));
}
Commit count: 1

cargo fmt