blob: d2cc076594af136852970fdf9f4e89250a5b4ee6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// Test for concurrent tasks
enum msg {
ready(comm::chan<msg>),
start,
done(int),
}
fn calc(children: uint, parent_ch: comm::chan<msg>) {
let port = comm::port();
let chan = comm::chan(port);
let mut child_chs = [];
let mut sum = 0;
iter::repeat (children) {||
task::spawn {||
calc(0u, chan);
};
}
iter::repeat (children) {||
alt check comm::recv(port) {
ready(child_ch) {
child_chs += [child_ch];
}
}
}
comm::send(parent_ch, ready(chan));
alt check comm::recv(port) {
start {
vec::iter (child_chs) { |child_ch|
comm::send(child_ch, start);
}
}
}
iter::repeat (children) {||
alt check comm::recv(port) {
done(child_sum) { sum += child_sum; }
}
}
comm::send(parent_ch, done(sum + 1));
}
fn main(args: [str]) {
let children = if vec::len(args) == 2u {
option::get(uint::from_str(args[1]))
} else {
100u
};
let port = comm::port();
let chan = comm::chan(port);
task::spawn {||
calc(children, chan);
};
alt check comm::recv(port) {
ready(chan) {
comm::send(chan, start);
}
}
let sum = alt check comm::recv(port) {
done(sum) { sum }
};
#error("How many tasks? %d tasks.", sum);
}
|