-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspmc.rs
37 lines (32 loc) · 1000 Bytes
/
spmc.rs
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
//! Write from one thread, read from multiple threads.
use chute::LendingReader;
fn main() {
const MESSAGES : usize = 400;
const READERS : usize = 4;
let mut queue = chute::spmc::Queue::new();
std::thread::scope(|s| {
// READ threads
for _ in 0..READERS {
let mut reader = queue.reader();
s.spawn(move || {
let mut sum = 0;
for _ in 0..MESSAGES {
// Wait for the next message.
let msg = loop {
if let Some(msg) = reader.next() {
break msg;
}
};
sum += msg;
}
assert_eq!(sum, (0..MESSAGES).sum());
});
}
// WRITE thread
s.spawn(|| {
for i in 0..MESSAGES {
queue.push(i);
}
});
});
}