#ring-buffer #spsc #ringbuffer-spsc

ringbuffer-spsc

A fast thread-safe single producer-single consumer ring buffer

12 releases

0.1.13 Feb 10, 2025
0.1.12 Feb 10, 2025
0.1.11 Jan 29, 2025
0.1.9 Dec 30, 2022
0.1.3 Jul 27, 2022

#95 in Concurrency

Download history 5453/week @ 2024-12-22 6893/week @ 2024-12-29 12237/week @ 2025-01-05 16910/week @ 2025-01-12 13056/week @ 2025-01-19 15324/week @ 2025-01-26 15785/week @ 2025-02-02 15559/week @ 2025-02-09 14351/week @ 2025-02-16 16438/week @ 2025-02-23 20217/week @ 2025-03-02 21336/week @ 2025-03-09 24033/week @ 2025-03-16 21857/week @ 2025-03-23 23275/week @ 2025-03-30 23930/week @ 2025-04-06

94,246 downloads per month
Used in 58 crates (via zenoh-transport)

EPL-2.0 license

10KB
119 lines

ringbuffer-spsc

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.

Example

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

Dependencies

~145KB