11 releases

new 0.1.11 Jan 29, 2025
0.1.10 Jan 29, 2025
0.1.9 Dec 30, 2022
0.1.6 Sep 22, 2022
0.1.3 Jul 27, 2022

#87 in Concurrency

Download history 13726/week @ 2024-10-09 15418/week @ 2024-10-16 15764/week @ 2024-10-23 12613/week @ 2024-10-30 12907/week @ 2024-11-06 13735/week @ 2024-11-13 12892/week @ 2024-11-20 12471/week @ 2024-11-27 15619/week @ 2024-12-04 16434/week @ 2024-12-11 10427/week @ 2024-12-18 5168/week @ 2024-12-25 9901/week @ 2025-01-01 12800/week @ 2025-01-08 16514/week @ 2025-01-15 11317/week @ 2025-01-22

51,382 downloads per month
Used in 52 crates (via zenoh-transport)

EPL-2.0 license

10KB
117 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

~150KB