-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathcrossbeam-deque.rs
67 lines (57 loc) · 1.47 KB
/
crossbeam-deque.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crossbeam_deque::{Steal, Worker};
use std::thread;
mod message;
const MESSAGES: usize = 5_000_000;
fn seq() {
let tx = Worker::new_lifo();
let rx = tx.stealer();
for i in 0..MESSAGES {
tx.push(message::new(i));
}
for _ in 0..MESSAGES {
match rx.steal() {
Steal::Success(_) => {}
Steal::Retry => panic!(),
Steal::Empty => panic!(),
}
}
}
fn spsc() {
let tx = Worker::new_lifo();
let rx = tx.stealer();
crossbeam::scope(|scope| {
scope.spawn(move |_| {
for i in 0..MESSAGES {
tx.push(message::new(i));
}
});
scope.spawn(move |_| {
for _ in 0..MESSAGES {
loop {
match rx.steal() {
Steal::Success(_) => break,
Steal::Retry | Steal::Empty => thread::yield_now(),
}
}
}
});
})
.unwrap();
}
fn main() {
macro_rules! run {
($name:expr, $f:expr) => {
let now = ::std::time::Instant::now();
$f;
let elapsed = now.elapsed();
println!(
"{:25} {:15} {:7.3} sec",
$name,
"Rust crossbeam-deque",
elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1e9
);
};
}
run!("unbounded_seq", seq());
run!("unbounded_spsc", spsc());
}