标签:res 位置 调用 suspend 主函数 exe local pre hit
co = coroutine.create(function() print("coroutine execute!") end)
print(coroutine.status(co))
输出:
suspended
coroutine.resume(co) print(coroutine.status(co))
输出:
dead
co = coroutine.create(function(a, b) print("coroutine execute!") return a + b end) print(coroutine.resume(co, 2, 3))
输出:
ture 5
co = coroutine.create(function() for i = 1, 2 do print("before", i) coroutine.yield() print("after", i) end end) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co)) coroutine.resume(co) print(coroutine.status(co))输出:
dead
co = coroutine.create(function(a) a = a + 1 coroutine.yield(a) a = a + 1 return a end) print(coroutine.status(co)) print(coroutine.resume(co,1)) print(coroutine.status(co)) print(coroutine.resume(co,1)) print(coroutine.status(co))输出:
function co_func(a) local c = coroutine.yield(a + 1) return c * 2 end co = coroutine.wrap(co_func) ret1 = co(1) print(ret1) ret2 = co(10) print(ret2)输出:
运行一次协程对象。协程就会被激活一次,再次调用时从上一次断点的地方继续运行。
挂起时yield的參数将作为返回值,再次激活时的參数将作为yield的返回值。
print("running:",coroutine.running()) co = coroutine.create(function() print("running:",coroutine.running()) print("is co:", coroutine.running() == co) print("status:", coroutine.status(coroutine.running())) end) coroutine.resume(co) print("running:",coroutine.running())输出:
标签:res 位置 调用 suspend 主函数 exe local pre hit
原文地址:http://www.cnblogs.com/claireyuancy/p/6691854.html