feat: add example multi chan

This commit is contained in:
2021-01-24 23:46:49 +08:00
parent 980d52ad07
commit a8c9c92f75
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
use std::thread;
use crossbeam_channel::bounded;
fn main() {
let (s, r) = bounded(0);
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();
}

View File

@@ -0,0 +1,8 @@
_:
@just --list
ex-multi-chan:
cargo r --example multi_chan