-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathPingPong2.dpr
63 lines (52 loc) · 1.24 KB
/
PingPong2.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
program PingPong2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
{$I ../Impl.inc}
{$I ../Includes.inc}
System.SysUtils;
type
TPingPongChannel = TChannel<Integer>;
const
PING_PONG_COUNT = 1000;
var
Channel: TPingPongChannel;
Pinger: TSymmetric<TPingPongChannel>;
Ponger: TSymmetric<TPingPongChannel>;
begin
// create channel for data transferring
Channel := TPingPongChannel.Make;
// create ping/pong workers
Pinger := TSymmetric<TPingPongChannel>.Spawn(
procedure(const Chan: TPingPongChannel)
var
Ping, Pong: Integer;
begin
for Ping := 1 to PING_PONG_COUNT do
begin
Chan.Write(Ping);
Chan.Read(Pong);
Assert(Ping = Pong);
end;
Chan.Close;
end,
// put channel as argument
Channel
);
Ponger := TSymmetric<TPingPongChannel>.Spawn(
procedure(const Chan: TPingPongChannel)
var
Ping: Integer;
begin
// channel will automatically break for in loop when channel is closed.
for Ping in Chan do
Chan.Write(Ping)
end,
// put channel as argument
Channel
);
// wait until ping-pong is terminated
Join([Pinger, Ponger]);
Write('Press any key');
ReadLn;
end.