You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
use std::thread;
|
|
use std::sync::mpsc::{channel};
|
|
use std::time;
|
|
|
|
fn main() {
|
|
let (pinginsend,pinginrecv) = channel();
|
|
let (pingoutsend,pingoutrecv) = channel();
|
|
let mut ping = 1;
|
|
thread::spawn(move || {
|
|
let t = time::Duration::from_millis(1000);
|
|
loop {
|
|
let n = pinginrecv.recv().unwrap();
|
|
ping += n;
|
|
println!("ping {}", ping);
|
|
thread::sleep(t);
|
|
pingoutsend.send(ping).unwrap();
|
|
}
|
|
});
|
|
|
|
let (ponginsend,ponginrecv) = channel();
|
|
let (pongoutsend,pongoutrecv) = channel();
|
|
let mut pong = 2;
|
|
thread::spawn(move || {
|
|
let t = time::Duration::from_millis(1000);
|
|
loop {
|
|
let n = ponginrecv.recv().unwrap();
|
|
pong += n;
|
|
println!("pong {}", pong);
|
|
thread::sleep(t);
|
|
pongoutsend.send(pong).unwrap();
|
|
}
|
|
});
|
|
|
|
let mut d = 3;
|
|
loop {
|
|
pinginsend.send(d).unwrap();
|
|
d = pingoutrecv.recv().unwrap();
|
|
ponginsend.send(d).unwrap();
|
|
d = pongoutrecv.recv().unwrap();
|
|
}
|
|
}
|