-
Notifications
You must be signed in to change notification settings - Fork 34
/
AsyncChannels.dpr
66 lines (55 loc) · 1.35 KB
/
AsyncChannels.dpr
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
program AsyncChannels;
{$APPTYPE CONSOLE}
{$R *.res}
uses
{$I ../Impl.inc}
{$I ../Includes.inc}
System.SysUtils;
type
TChannel = TChannel<Integer>;
const
DATA_COUNT = 10;
var
AsyncChannel: TChannel;
Producer: TSymmetric<TChannel>;
Consumer: TSymmetric<TChannel>;
begin
// create assync channel (non-zero buffer)
AsyncChannel := TChannel.Make(5);
// create producer/consumer workers
Producer := TSymmetric<TChannel>.Spawn(
procedure(const Chan: TChannel)
var
Data: Integer;
begin
for Data := 1 to DATA_COUNT do
begin
WriteLn(Format('-> Producer: send:%d', [Data]));
Chan.Write(Data);
WriteLn(Format('-> Producer: sended:%d', [Data]));
end;
WriteLn('-> Producer: close channel');
Chan.Close;
end,
// put channel as argument
AsyncChannel
);
Consumer := TSymmetric<TChannel>.Spawn(
procedure(const Chan: TChannel)
var
Ping: Integer;
begin
while Chan.Read(Ping) do
begin
WriteLn(Format('<- Consumer: recieved:%d', [Ping]));
end;
WriteLn('<- Consumer: channel is closed');
end,
// put channel as argument
AsyncChannel
);
// wait until ping-pong is terminated
Join([Consumer, Producer]);
Write('Press any key');
ReadLn
end.