2011-09-20 13:47:22 -05:00
|
|
|
use std;
|
2011-12-13 18:25:51 -06:00
|
|
|
import comm;
|
|
|
|
import task;
|
2011-09-20 13:47:22 -05:00
|
|
|
|
|
|
|
fn main() {
|
2011-10-13 17:37:07 -05:00
|
|
|
let po = comm::port::<int>();
|
2011-07-27 07:48:34 -05:00
|
|
|
|
|
|
|
// Spawn 10 tasks each sending us back one int.
|
2011-09-20 13:47:22 -05:00
|
|
|
let i = 10;
|
2011-07-27 07:48:34 -05:00
|
|
|
while (i > 0) {
|
|
|
|
log i;
|
2011-10-13 23:23:07 -05:00
|
|
|
task::spawn((i, comm::chan(po)), child);
|
2011-07-27 07:48:34 -05:00
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawned tasks are likely killed before they get a chance to send
|
|
|
|
// anything back, so we deadlock here.
|
|
|
|
|
|
|
|
i = 10;
|
2011-09-20 13:47:22 -05:00
|
|
|
let value = 0;
|
2011-07-27 07:48:34 -05:00
|
|
|
while (i > 0) {
|
|
|
|
log i;
|
2011-09-20 13:47:22 -05:00
|
|
|
value = comm::recv(po);
|
2011-07-27 07:48:34 -05:00
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "main thread exiting";
|
|
|
|
}
|
|
|
|
|
2011-10-20 22:34:04 -05:00
|
|
|
fn child(&&args: (int, comm::chan<int>)) {
|
2011-10-13 17:37:07 -05:00
|
|
|
let (x, ch) = args;
|
2011-07-27 07:48:34 -05:00
|
|
|
log x;
|
2011-09-20 13:47:22 -05:00
|
|
|
comm::send(ch, x);
|
2011-07-27 07:48:34 -05:00
|
|
|
}
|