forked from Darredevil/D-Fibers
-
Notifications
You must be signed in to change notification settings - Fork 3
/
channels.d
61 lines (56 loc) · 1.31 KB
/
channels.d
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
/ dub.json:
{
"authors": [
"Dmitry Olshansky"
],
"copyright": "Copyright © 2024, Dmitry Olshansky",
"dependencies": {
"photon": { "path": ".." }
},
"description": "A test for channels API",
"license": "BOOST",
"name": "channels"
}
/
module examples.channels;
import std.algorithm, std.datetime, std.range, std.stdio;
import photon;
void first(shared Channel!string work, shared Channel!int completion) {
delay(2.msecs);
work.put("first #1");
delay(2.msecs);
work.put("first #2");
delay(2.msecs);
work.put("first #3");
completion.put(1);
}
void second(shared Channel!string work, shared Channel!int completion) {
delay(3.msecs);
work.put("second #1");
delay(3.msecs);
work.put("second #2");
completion.put(2);
}
void main() {
startloop();
auto jobQueue = channel!string(2);
auto finishQueue = channel!int(1);
go({
first(jobQueue, finishQueue);
});
go({ // producer # 2
second(jobQueue, finishQueue);
});
go({ // consumer
foreach (item; jobQueue) {
delay(1.seconds);
writeln(item);
}
});
go({ // closer
auto completions = finishQueue.take(2).array;
assert(completions.length == 2);
jobQueue.close(); // all producers are done
});
runFibers();
}