-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexample2.lua
More file actions
29 lines (25 loc) · 772 Bytes
/
Copy pathexample2.lua
File metadata and controls
29 lines (25 loc) · 772 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
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, 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
concurrent.send(pid, { from = concurrent.self(), body = 'finished' })
print('ping finished')
end
pid = concurrent.spawn(pong)
concurrent.spawn(ping, 3, pid)
concurrent.loop()