Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmr/408114fdac6fd4c2c8e8 to your computer and use it in GitHub Desktop.
Save ryanmr/408114fdac6fd4c2c8e8 to your computer and use it in GitHub Desktop.
fn multi() {
let tasks:uint = 4;
let (tx, rx): (Sender<uint>, Receiver<uint>) = channel();
let mut senders = Vec::<Sender<uint>>::new();
let mut receivers = Vec::<Receiver<uint>>::new();
for i in range(0, tasks) {
let task_tx = tx.clone();
let (ctx, crx): (Sender<uint>, Receiver<uint>) = channel();
senders.push(ctx);
spawn(proc() {
let task_id = i;
let mut iterations = 0;
loop {
let result = crx.try_recv();
match result {
Ok(r) => {
if r == 1 {
println!("tid: {} stop task", task_id);
break;
}
},
Err(e) => {}
}
game(); // simulation of game
iterations = iterations + 1;
task_tx.send(iterations);
}
println!("tid: {} task ended", task_id);
});
}
let mut total = 0u;
loop {
total = 0;
for _ in range(0, tasks) {
total = total + rx.recv();
}
println!("total games: {}", total);
// attempt to end the tasks
if total > 5000 {
for i in range(0, tasks) {
senders[i].send(1);
}
break;
}
}
}
fn main() {
// game();
multi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment