Crates.io | ringbuffer-spsc |
lib.rs | ringbuffer-spsc |
version | 0.1.9 |
source | src |
created_at | 2022-07-27 09:44:03.878371 |
updated_at | 2022-12-30 09:04:27.643831 |
description | A fast thread-safe single producer-single consumer ring buffer |
homepage | |
repository | https://github.com/Mallets/ringbuffer-spsc |
max_upload_size | |
id | 633803 |
size | 25,462 |
A fast single-producer single-consumer ring buffer. For performance reasons, the capacity of the buffer is determined at compile time via a const generic and it is required to be a power of two for a more efficient index handling.
use ringbuffer_spsc::RingBuffer;
fn main() {
const N: usize = 1_000_000;
let (mut tx, mut rx) = RingBuffer::<usize, 16>::new();
let p = std::thread::spawn(move || {
let mut current: usize = 0;
while current < N {
if tx.push(current).is_none() {
current = current.wrapping_add(1);
} else {
std::thread::yield_now();
}
}
});
let c = std::thread::spawn(move || {
let mut current: usize = 0;
while current < N {
if let Some(c) = rx.pull() {
assert_eq!(c, current);
current = current.wrapping_add(1);
} else {
std::thread::yield_now();
}
}
});
p.join().unwrap();
c.join().unwrap();
}