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

#106 in Concurrency

Download history 4483/week @ 2023-12-05 5330/week @ 2023-12-12 4751/week @ 2023-12-19 2608/week @ 2023-12-26 4432/week @ 2024-01-02 5291/week @ 2024-01-09 5973/week @ 2024-01-16 9304/week @ 2024-01-23 10111/week @ 2024-01-30 12085/week @ 2024-02-06 11145/week @ 2024-02-13 7984/week @ 2024-02-20 10050/week @ 2024-02-27 11176/week @ 2024-03-05 10067/week @ 2024-03-12 9109/week @ 2024-03-19

42,004 downloads per month
Used in 28 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