Lua协程(coroutine)程序运行分析
这是一段分析lua协程(协同程序,coroutine)的代码,来自Luareferencemanualinterface(略有修改):
functionfoo(a) print("foo",a) returncoroutine.yield(2*a) end
co=coroutine.create(function(a,b) print("co-body1",a,b) localr=foo(a+1) print("co-body2",r) localr,s=coroutine.yield(a+b,a-b) print("co-body3",r,s) returnb,"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"))