-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel_worker.ion
More file actions
39 lines (33 loc) · 968 Bytes
/
Copy pathchannel_worker.ion
File metadata and controls
39 lines (33 loc) · 968 Bytes
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
// Channel worker: main sends job ids on a channel; a spawned thread sums them and replies.
fn job_count() -> int {
return 5;
}
fn main() -> int {
let (job_tx, job_rx): (Sender<int>, Receiver<int>) = channel<int>();
let (done_tx, done_rx): (Sender<int>, Receiver<int>) = channel<int>();
spawn {
let mut rx: Receiver<int> = job_rx;
let mut sum: int = 0;
let mut received: int = 0;
loop {
if received >= job_count() {
break;
}
let job: int = recv(&mut rx);
sum = sum + job;
received = received + 1;
}
send(&done_tx, sum);
};
let mut next_job: int = 1;
while next_job <= job_count() {
send(&job_tx, next_job);
next_job = next_job + 1;
}
let mut done_rx_mut: Receiver<int> = done_rx;
let total: int = recv(&mut done_rx_mut);
if total != 15 {
return 1;
}
return 0;
}