#单消费者 #环形缓冲区 #单生产者 #线程安全 #编译时 #SPSC #性能

ringbuffer-spsc

一个快速线程安全的单生产者单消费者环形缓冲区

9个版本

0.1.9 2022年12月30日
0.1.8 2022年12月12日
0.1.6 2022年9月22日
0.1.3 2022年7月27日

#224并发

Download history 9764/week @ 2024-04-22 8752/week @ 2024-04-29 7332/week @ 2024-05-06 13762/week @ 2024-05-13 8373/week @ 2024-05-20 8321/week @ 2024-05-27 8693/week @ 2024-06-03 9014/week @ 2024-06-10 9227/week @ 2024-06-17 9813/week @ 2024-06-24 8638/week @ 2024-07-01 9106/week @ 2024-07-08 8754/week @ 2024-07-15 9800/week @ 2024-07-22 8884/week @ 2024-07-29 9815/week @ 2024-08-05

37,702 每月下载量
用于 40 个Crates(通过 zenoh-transport

EPL-2.0 许可证

9KB
105

ringbuffer-spsc

一个快速的单生产者单消费者环形缓冲区。出于性能考虑,缓冲区的容量在编译时通过const泛型确定,并且要求为2的幂,以便更有效地处理索引。

示例

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

依赖项

~33KB