这篇文章主要介绍了Lua协同程序(COROUTINE)运行步骤分解,本文着重分解协同程序的运行步骤,需要的朋友可以参考下
这是一段分析 lua 协程(协同程序,coroutine)的代码,来自 Lua reference manual interface (略有修改):
复制代码 代码如下:
function foo (a)
print("foo", a)
return coroutine.yield(2*a)
end
co = coroutine.create(function (a,b)
print("co-body1", a, b)
local r = foo(a+1)
print("co-body2", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body3", r, s)
return b, "end"
end)
print("1----")
print("main", coroutine.resume(co, 1, 10))
print("2----")
print("main", coroutine.resume(co, "r"))
print("3----")
print("main", coroutine.resume(co, "x", "y"))
print("4----")
print("main", coroutine.resume(co, "x", "y"))
以上就是Lua协同程序(COROUTINE)运行步骤分解的详细内容,更多请关注0133技术站其它相关文章!