Created
November 19, 2014 09:48
-
-
Save nkoneko/b3102081b316de8d8462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::Timer; | |
use std::io::timer; | |
use std::time::duration::Duration; | |
fn main() { | |
let (sc1, rc1) = channel(); | |
let (sc2, rc2) = channel(); | |
let mut timer = Timer::new().unwrap(); | |
let timeout1 = timer.oneshot(Duration::milliseconds(1000)); | |
spawn(proc() { | |
let (pong_s, pong_r) = channel(); | |
sc1.send(pong_s); | |
let ping_s: Sender<Option<String>> = rc2.recv(); | |
loop { | |
ping_s.send(Some("Ping".to_string())); | |
select! ( | |
maybe_pong = pong_r.recv() => | |
match maybe_pong { | |
Some(pong) => println!("{}", pong), | |
None => break | |
}, | |
() = timeout1.recv() => { | |
ping_s.send(None); | |
break | |
} | |
); | |
}; | |
}); | |
spawn(proc() { | |
let (ping_s, ping_r) = channel(); | |
let pong_s= rc1.recv(); | |
sc2.send(ping_s); | |
loop { | |
match ping_r.recv() { | |
Some(ping) => { | |
println!("{}", ping); | |
pong_s.send_opt(Some("Pong".to_string())); | |
}, | |
None => break | |
}; | |
}; | |
}); | |
timer::sleep(Duration::milliseconds(1500)); | |
println!("おわり"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment