12 releases

new 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

#92 in Concurrency

Download history 13817/week @ 2024-10-26 13168/week @ 2024-11-02 12796/week @ 2024-11-09 13409/week @ 2024-11-16 11463/week @ 2024-11-23 15327/week @ 2024-11-30 17015/week @ 2024-12-07 13458/week @ 2024-12-14 5647/week @ 2024-12-21 6742/week @ 2024-12-28 12178/week @ 2025-01-04 16806/week @ 2025-01-11 13063/week @ 2025-01-18 15339/week @ 2025-01-25 15742/week @ 2025-02-01 15547/week @ 2025-02-08

63,259 downloads per month
Used in 52 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