打印输出:
suspended
running
coroutine section 1 1 2 10
main true 2 3
suspended
coroutine section 2 11 nil 11
main true 3 -1
suspended
coroutine section 3 5 6 12
main true 2 end
dead
main false cannot resume dead coroutine
dead
wrap的例子:
local func = function()
coroutine.yield(1)
coroutine.yield(2)
coroutine.yield(3)
coroutine.yield(4)
coroutine.yield(5)
end
local cofun = coroutine.wrap (func)
for i = 1, 7 do
print(cofun())
end
打印结果:
1
2
3
4
5
LuaException: xxxxxxxxxx: cannot resume dead coroutine
注1:第六行显示的是空,因为func方法里实际上执行了六次,第六次的返回值为nil。