Programming in Lua - 9.1
Programming in Lua - 9.1
Programming in Lua
Part I. The Language
Chapter 9. Coroutines
co = coroutine.create(function ()
print("hi")
end)
coroutine.resume(co) --> hi
In this example,
the coroutine body simply prints "hi" and terminates,
leaving the coroutine in the dead state,
from which it cannot return:
co = coroutine.create(function ()
for i=1,10 do
print("co", i)
coroutine.yield()
end
end)
coroutine.resume(co) --> co 1
coroutine.resume(co) --> co 2
coroutine.resume(co) --> co 3
...
coroutine.resume(co) --> co 10
print(coroutine.resume(co))
co = coroutine.create(function (a,b,c)
print("co", a,b,c)
end)
coroutine.resume(co, 1, 2, 3) --> co 1 2 3
coroutine.yield(a + b, a - b)
end)
co = coroutine.create (function ()
print("co", coroutine.yield())
end)
coroutine.resume(co)
coroutine.resume(co, 4, 5) --> co 4 5
co = coroutine.create(function ()
return 6, 7
end)