#ringbuffer #spsc #c #ring-buffer

ringbuffer-spsc

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

9 releases

0.1.9 Dec 30, 2022
0.1.8 Dec 12, 2022
0.1.6 Sep 22, 2022
0.1.3 Jul 27, 2022

#67 in Caching

Download history 3238/week @ 2023-06-06 3046/week @ 2023-06-13 2868/week @ 2023-06-20 3168/week @ 2023-06-27 2896/week @ 2023-07-04 2823/week @ 2023-07-11 2695/week @ 2023-07-18 2815/week @ 2023-07-25 2928/week @ 2023-08-01 2953/week @ 2023-08-08 2716/week @ 2023-08-15 3174/week @ 2023-08-22 3079/week @ 2023-08-29 3447/week @ 2023-09-05 3268/week @ 2023-09-12 2791/week @ 2023-09-19

13,164 downloads per month
Used in 23 crates (via zenoh-transport)

EPL-2.0 license

9KB
105 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

~33KB