Crates.io | atb |
lib.rs | atb |
version | 0.1.0 |
source | src |
created_at | 2022-10-08 01:26:41.580414 |
updated_at | 2022-10-08 01:26:41.580414 |
description | Simple lock-free triple buffer |
homepage | https://github.com/jf2048/atb |
repository | https://github.com/jf2048/atb.git |
max_upload_size | |
id | 683234 |
size | 15,999 |
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.
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));
}