Last active
August 29, 2015 14:12
-
-
Save ryanmr/408114fdac6fd4c2c8e8 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
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