-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
38 lines (34 loc) · 1.08 KB
/
Copy pathbasic.rs
File metadata and controls
38 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use spsc_ring::ring;
use std::thread;
fn main() {
let (tx, rx) = ring::<u64>(64).unwrap();
let producer = thread::spawn(move || {
for i in 0..100u64 {
loop {
match tx.try_push(i) {
Ok(()) => break,
Err(spsc_ring::TrySendError::Full(_)) => std::hint::spin_loop(),
Err(spsc_ring::TrySendError::Disconnected(_)) => return,
}
}
}
println!("produced 100 items");
});
let consumer = thread::spawn(move || {
let mut received = Vec::with_capacity(100);
while received.len() < 100 {
match rx.try_pop() {
Ok(v) => received.push(v),
Err(spsc_ring::TryRecvError::Empty) => std::hint::spin_loop(),
Err(spsc_ring::TryRecvError::Disconnected) => break,
}
}
println!(
"consumed {} items, last={}",
received.len(),
received.last().unwrap()
);
});
producer.join().unwrap();
consumer.join().unwrap();
}