[예제]
--suspended, running, dead, normal
--coroutine is similar to thread in multi-thread
co = coroutine.create(function() print("hi") end)
print(co)
print(coroutine.status(co))
coroutine.resume(co)
print(coroutine.status(co))
co = coroutine.create(function()
print(coroutine.status(co))
for i = 1, 10 do
print("co", i)
coroutine.yield()
end
end)
coroutine.resume(co)
print(coroutine.status(co))
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
print(coroutine.resume(co))
print(coroutine.resume(co)) --이 줄 부터 아무것도 출력하지 않음
--resume과 yield를 이용한 데이터 전달
co = coroutine.create(function(a, b, c)
print("co", a, b, c +2)
end)
coroutine.resume(co, 1, 2, 3)
co = coroutine.create(function(a,b)
coroutine.yield(a + b, a - b)
end)
print(coroutine.resume(co, 20, 10))
co = coroutine.create(function(x)
print("co1", x)
print("co2", coroutine.yield())
end)
coroutine.resume(co, "hi")
coroutine.resume(co, 4, 5)
co = coroutine.create(function()
return 6, 7
end)
print(coroutine.resume(co))
--[[
function producer()
while true do
local x = io.read()
send(x)
end
end
--]]
function consumer()
while true do
local x = receive()
io.write(x, "\n")
end
end
function receive()
local status, value = coroutine.resume(producer)
return value
end
function send(x)
coroutine.yield(x)
end
producer = coroutine.create(
function()
while true do
local x = io.read()
send(x)
end
end)
--Using filter Producer-Consumder
[결과]
thread: 0000000000605310
suspended
hi
dead
running
co 1
suspended
co 2
co 3
co 4
co 5
co 6
co 7
co 8
co 9
co 10
true
false cannot resume dead coroutine
co 1 2 5
true 30 10
co1 hi
co2 4 5
true 6 7
--suspended, running, dead, normal
--coroutine is similar to thread in multi-thread
co = coroutine.create(function() print("hi") end)
print(co)
print(coroutine.status(co))
coroutine.resume(co)
print(coroutine.status(co))
co = coroutine.create(function()
print(coroutine.status(co))
for i = 1, 10 do
print("co", i)
coroutine.yield()
end
end)
coroutine.resume(co)
print(coroutine.status(co))
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
print(coroutine.resume(co))
print(coroutine.resume(co)) --이 줄 부터 아무것도 출력하지 않음
--resume과 yield를 이용한 데이터 전달
co = coroutine.create(function(a, b, c)
print("co", a, b, c +2)
end)
coroutine.resume(co, 1, 2, 3)
co = coroutine.create(function(a,b)
coroutine.yield(a + b, a - b)
end)
print(coroutine.resume(co, 20, 10))
co = coroutine.create(function(x)
print("co1", x)
print("co2", coroutine.yield())
end)
coroutine.resume(co, "hi")
coroutine.resume(co, 4, 5)
co = coroutine.create(function()
return 6, 7
end)
print(coroutine.resume(co))
--[[
function producer()
while true do
local x = io.read()
send(x)
end
end
--]]
function consumer()
while true do
local x = receive()
io.write(x, "\n")
end
end
function receive()
local status, value = coroutine.resume(producer)
return value
end
function send(x)
coroutine.yield(x)
end
producer = coroutine.create(
function()
while true do
local x = io.read()
send(x)
end
end)
--Using filter Producer-Consumder
[결과]
thread: 0000000000605310
suspended
hi
dead
running
co 1
suspended
co 2
co 3
co 4
co 5
co 6
co 7
co 8
co 9
co 10
true
false cannot resume dead coroutine
co 1 2 5
true 30 10
co1 hi
co2 4 5
true 6 7
[LUA] Chap 9. Coroutine
Reviewed by kukanuc
on
4월 16, 2019
Rating:
댓글 없음: