-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpsc.rs
61 lines (58 loc) · 1.56 KB
/
mpsc.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
use std::thread;
use std::env::args;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Instant;
fn sender(tx: Sender<u64>, data: u64, mut limit: u64) {
loop {
if limit == 0 { break; }
limit -= 1;
tx.send(data).unwrap();
}
}
fn receiver(rx: Receiver<u64>) {
let mut counter = vec![0, 0, 0];
loop {
match rx.recv().unwrap() {
0 => counter[0] += 1,
1 => counter[1] += 1,
2 => counter[2] += 1,
_ => break
}
}
for (idx, data) in counter.iter().enumerate() {
println!("thread {} => {}", idx, data);
}
println!("receiver done.");
}
fn main() {
if args().len() < 2 {
println!("{} num", args().nth(0).unwrap());
std::process::exit(1);
}
let num: u64 = args().nth(1).unwrap().parse().unwrap();
let (tx, rx) = channel();
let (tx1, tx2, tx3) = (tx.clone(), tx.clone(), tx.clone());
let now = Instant::now();
let rcv = thread::spawn(move || {
receiver(rx);
});
let tid1 = thread::spawn(move || {
sender(tx1, 0, num);
println!("thread 0 done");
});
let tid2 = thread::spawn(move || {
sender(tx2, 1, num);
println!("thread 1 done");
});
let tid3 = thread::spawn(move || {
sender(tx3, 2, num);
println!("thread 2 done");
});
tid1.join().unwrap();
tid2.join().unwrap();
tid3.join().unwrap();
tx.send(309).unwrap();
rcv.join().unwrap();
let dur = now.elapsed();
println!("{}.{}", dur.as_secs(), dur.subsec_nanos());
}