atb 0.1.0

Simple lock-free triple buffer
Documentation
  • Coverage
  • 92.86%
    13 out of 14 items documented0 out of 12 items with examples
  • Size
  • Source code size: 15.9 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • jf2048/atb
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jf2048

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));
}