Skip to content

Instantly share code, notes, and snippets.

@ryanmr

ryanmr/simul.rs

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.