lean2/tests/lua/threads/th7.lua
Leonardo de Moura 5e170ce5fe chore(tests/lua): move examples that use threads to tests/lua/threads
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2013-11-15 09:11:58 -08:00

37 lines
816 B
Lua

S = State()
T = thread(S, [[
print("starting thread...")
pcall(function()
while true do
check_interrupted() -- check if thread was interrupted
local ok, val = read(10) -- 10 ms timeout
if ok then
print("thread received:", val)
write(val + 10, val - 10) -- send result back to main thread
end
end
end)
]])
for i = 1, 10 do
T:write(10 * i)
local r1 = T:read()
local r2 = T:read()
print("main received: ", r1, r2)
end
T:interrupt()
print("done")
-- Channels are quite flexible, we can send closure over the channel
T = thread(S, [[
for i = 1, 10 do
local f = read()
-- send back the result of f(i)
write(f(i))
end
]])
for i = 1, 10 do
T:write(function (x) return x + i end)
print(T:read())
end