forked from lefcha/concurrentlua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5b.lua
More file actions
32 lines (27 loc) · 793 Bytes
/
example5b.lua
File metadata and controls
32 lines (27 loc) · 793 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
concurrent = require 'concurrent'
concurrent.setoption('trapexit', true)
function pong()
while true do
local msg = concurrent.receive()
if msg.signal == 'EXIT' then
break
elseif msg.body == 'ping' then
print('pong received ping')
concurrent.send(msg.from, { body = 'pong' })
end
end
print('pong finished')
end
function ping(n, pid)
concurrent.link(pid)
for i = 1, n do
concurrent.send(pid, { from = concurrent.self(), body = 'ping' })
local msg = concurrent.receive()
if msg.body == 'pong' then print('ping received pong') end
end
print('ping finished')
concurrent.exit('finished')
end
pid = concurrent.spawn(pong)
concurrent.spawn(ping, 3, pid)
concurrent.loop()