co = coroutine.create(function() print("coroutine execute!") end)
print(coroutine.status(co))
输出:
suspended
coroutine.resume(co) print(coroutine.status(co))
输出:
running
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))输出:
resumer = coroutine.wrap(function(a) a = a + 1 print("before") coroutine.yield(a) print("after") a = a + 1 return a end) print(resumer(1)) print(resumer(2))输出:
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())输出:
原文地址:http://blog.csdn.net/xufeng0991/article/details/39894983