diff --git a/__concurrent/crossbeam_n_parking_lot/examples/multi_chan.rs b/__concurrent/crossbeam_n_parking_lot/examples/multi_chan.rs new file mode 100644 index 0000000..2184737 --- /dev/null +++ b/__concurrent/crossbeam_n_parking_lot/examples/multi_chan.rs @@ -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(); +} diff --git a/__concurrent/crossbeam_n_parking_lot/justfile b/__concurrent/crossbeam_n_parking_lot/justfile new file mode 100644 index 0000000..5bbdd83 --- /dev/null +++ b/__concurrent/crossbeam_n_parking_lot/justfile @@ -0,0 +1,8 @@ +_: + @just --list + +ex-multi-chan: + cargo r --example multi_chan + + +