Files
simple-rust-tests/__concurrent/crossbeam_n_parking_lot/examples/multi_chan.rs
2021-01-24 23:48:44 +08:00

38 lines
884 B
Rust

use std::thread;
use crossbeam_channel::bounded;
fn main() {
let (s, r) = bounded(100);
let mut ts = vec![];
for ti in 0..4 {
let copied_sender = s.clone();
let t = thread::spawn(move|| {
println!("Start: {}", ti);
for i in 0..100000_u64 {
copied_sender.send(Some(i)).unwrap();
}
println!("End: {}", ti);
});
ts.push(t);
}
let tt = thread::spawn(move || {
println!("Start collect");
let mut sum = 0_u64;
loop {
let x = r.recv().unwrap();
match x {
None => break,
Some(x) => sum+=x,
}
}
println!("Enc collect");
println!("Sum: {}", sum);
});
for t in ts {
t.join().unwrap();
}
s.send(None).unwrap();
tt.join().unwrap();
}