rust/src/test/run-pass/task-comm-14.rs

35 lines
665 B
Rust
Raw Normal View History

2011-09-20 13:47:22 -05:00
use std;
import comm;
import task;
2011-09-20 13:47:22 -05:00
fn main() {
let po = comm::port::<int>();
2012-01-04 23:14:53 -06:00
let ch = comm::chan(po);
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(debug, i);
2012-01-04 23:14:53 -06:00
task::spawn {|| child(i, ch); };
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(debug, i);
2011-09-20 13:47:22 -05:00
value = comm::recv(po);
2011-07-27 07:48:34 -05:00
i = i - 1;
}
#debug("main thread exiting");
2011-07-27 07:48:34 -05:00
}
2012-01-04 23:14:53 -06:00
fn child(x: int, ch: comm::chan<int>) {
log(debug, x);
2012-01-04 23:14:53 -06:00
comm::send(ch, copy x);
2011-07-27 07:48:34 -05:00
}