throughput/
throughput.rs

1use ringbuffer_spsc::RingBuffer;
2use std::sync::atomic::{AtomicUsize, Ordering};
3use std::sync::Arc;
4use std::time::Duration;
5
6fn main() {
7    let (mut tx, mut rx) = RingBuffer::<usize, 16>::init();
8    let counter = Arc::new(AtomicUsize::new(0));
9
10    std::thread::spawn(move || {
11        let mut current: usize = 0;
12        loop {
13            if tx.push(current).is_none() {
14                current = current.wrapping_add(1);
15            } else {
16                std::thread::yield_now();
17            }
18        }
19    });
20
21    let c_counter = counter.clone();
22    std::thread::spawn(move || {
23        let mut current: usize = 0;
24        loop {
25            if let Some(c) = rx.pull() {
26                assert_eq!(c, current);
27                current = current.wrapping_add(1);
28                c_counter.fetch_add(1, Ordering::Relaxed);
29            } else {
30                std::thread::yield_now();
31            }
32        }
33    });
34
35    loop {
36        std::thread::sleep(Duration::from_secs(1));
37        println!("{} elem/s", counter.swap(0, Ordering::Relaxed));
38    }
39}