use gil::mpmc::sharded::channel;
use std::{hint::black_box, num::NonZeroUsize, thread::spawn, time::SystemTime};
fn main() {
const SENDERS: usize = 8;
const RECEIVERS: usize = 8;
const CAPACITY_PER_SHARD: usize = 4096;
const MESSAGES: usize = 1_000_000;
let (mut tx, mut rx) = channel(
NonZeroUsize::new(SENDERS).unwrap(),
NonZeroUsize::new(CAPACITY_PER_SHARD).unwrap(),
);
let start = SystemTime::now();
let mut sender_handles = Vec::with_capacity(SENDERS);
for _ in 0..SENDERS - 1 {
let mut tx = tx.try_clone().expect("too many senders for max_shards");
sender_handles.push(spawn(move || {
for i in 0..MESSAGES {
tx.send(black_box(i));
}
}));
}
sender_handles.push(spawn(move || {
for i in 0..MESSAGES {
tx.send(black_box(i));
}
}));
let mut receiver_handles = Vec::with_capacity(RECEIVERS);
for _ in 0..RECEIVERS - 1 {
let mut rx = rx.try_clone().expect("too many receivers for max_shards");
receiver_handles.push(spawn(move || {
for _ in 0..(SENDERS * MESSAGES / RECEIVERS) {
let x = rx.recv();
black_box(x);
}
}));
}
receiver_handles.push(spawn(move || {
for _ in 0..(SENDERS * MESSAGES / RECEIVERS) {
let x = rx.recv();
black_box(x);
}
}));
for handle in sender_handles {
handle.join().unwrap();
}
for handle in receiver_handles {
handle.join().unwrap();
}
let time = start.elapsed().unwrap();
println!("{time:?}");
}