nchannel

shared(NChannel!(N, T))
nchannel
@property
(
size_t N
T...
)
(
)

Examples

1 import std.concurrency;
2 import std.conv;
3 
4 auto ch1 = channel!(int, long, string);
5 auto ch2 = channel!(int, long, string);
6 
7 static void spawnFuncSender(shared ch1.Sender s)
8 {
9     foreach(i; 0 .. 100){
10         s.put(cast(int)i);
11         s.put(i + (long.max >> 1) + 1);
12         s.put(to!string(i));
13     }
14 }
15 
16 
17 static void spawnFuncMiddle(shared ch1.Receiver r,
18                             shared ch2.Sender s)
19 {
20     foreach(i; 0 .. 100){
21         foreach(E; TypeTuple!(int, long, string))
22         {
23             while(r.queue!E.empty){}
24             s.put!E(*r.pop!E);
25         }
26     }
27 }
28 
29 spawn(&spawnFuncSender, ch1.sender);
30 spawn(&spawnFuncMiddle, ch1.receiver,
31                         ch2.sender);
32 
33 foreach(i; 0 .. 100){
34     foreach(E; TypeTuple!(int, long, string))
35     {
36         while(ch2.queue!E.empty){}
37 
38       static if(is(E == int))
39         assert(*ch2.pop!E == i);
40       else static if(is(E == long))
41         assert(*ch2.pop!E == i + (long.max >> 1) + 1);
42       else
43         assert(*ch2.pop!E == i.to!string);
44     }
45 }

Meta