2011-09-20 11:47:22 -07:00
|
|
|
use std;
|
2011-12-13 16:25:51 -08:00
|
|
|
import comm;
|
|
|
|
import task;
|
2011-09-20 11:47:22 -07:00
|
|
|
|
|
|
|
fn main() {
|
2011-10-13 15:37:07 -07:00
|
|
|
let po = comm::port::<int>();
|
2012-01-04 21:14:53 -08:00
|
|
|
let ch = comm::chan(po);
|
2011-07-27 14:48:34 +02:00
|
|
|
|
|
|
|
// Spawn 10 tasks each sending us back one int.
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut i = 10;
|
2011-07-27 14:48:34 +02:00
|
|
|
while (i > 0) {
|
2011-12-22 17:53:53 -08:00
|
|
|
log(debug, i);
|
2012-05-07 11:31:57 -07:00
|
|
|
task::spawn {|copy i| child(i, ch); };
|
2011-07-27 14:48:34 +02:00
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawned tasks are likely killed before they get a chance to send
|
|
|
|
// anything back, so we deadlock here.
|
|
|
|
|
|
|
|
i = 10;
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut value = 0;
|
2011-07-27 14:48:34 +02:00
|
|
|
while (i > 0) {
|
2011-12-22 17:53:53 -08:00
|
|
|
log(debug, i);
|
2011-09-20 11:47:22 -07:00
|
|
|
value = comm::recv(po);
|
2011-07-27 14:48:34 +02:00
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:42:52 -08:00
|
|
|
#debug("main thread exiting");
|
2011-07-27 14:48:34 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 21:14:53 -08:00
|
|
|
fn child(x: int, ch: comm::chan<int>) {
|
2011-12-22 17:53:53 -08:00
|
|
|
log(debug, x);
|
2012-01-04 21:14:53 -08:00
|
|
|
comm::send(ch, copy x);
|
2011-07-27 14:48:34 +02:00
|
|
|
}
|