#[macro_use]
extern crate chan;
use std::thread;
use std::time::Duration;
fn main() {
let (s1, r1) = chan::sync(0);
let (s2, r2) = chan::sync(0);
let (s1, s2) = (s1.clone(), s2.clone());
let wg = chan::WaitGroup::new();
wg.add(2);
let (wg1, wg2) = (wg.clone(), wg.clone());
thread::spawn(move || {
for i in 0..10 {
s1.send(i);
thread::sleep(Duration::from_millis(10));
}
wg1.done();
});
thread::spawn(move || {
for i in 0..10 {
s2.send(i * 5);
thread::sleep(Duration::from_millis(100));
}
wg2.done();
});
let done = {
let (s, r) = chan::sync(0);
thread::spawn(move || {
wg.wait();
s.send(());
});
r
};
loop {
let (r1b, r2b) = (&r1, &r2);
chan_select! {
default => {
chan_select! {
r1b.recv() -> v1 => match v1 {
None => {}
Some(v1) => println!("r1: {:?}", v1),
},
r2b.recv() -> v2 => match v2 {
None => {}
Some(v2) => println!("r2: {:?}", v2),
},
done.recv() => break,
}
},
r1.recv() -> v1 => match v1 {
None => {}
Some(v1) => println!("r1: {:?}", v1),
},
}
}
}