-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathexample3.lua
More file actions
30 lines (26 loc) · 801 Bytes
/
Copy pathexample3.lua
File metadata and controls
30 lines (26 loc) · 801 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
concurrent = require 'concurrent'
function pong()
while true do
local msg = concurrent.receive()
if msg.body == 'finished' 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)
for i = 1, n do
concurrent.send('pong', { from = concurrent.self(), body = 'ping' })
local msg = concurrent.receive()
if msg.body == 'pong' then print('ping received pong') end
end
concurrent.send('pong', { from = concurrent.self(), body = 'finished' })
print('ping finished')
end
pid = concurrent.spawn(pong)
concurrent.register('pong', pid)
concurrent.spawn(ping, 3)
concurrent.loop()