在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 print("Lua 协程测试2") 2 3 function testFun(n) 4 print("into foo,n = "..n) 5 return coroutine.yield(10 * n) -- 挂起co协程 6 end 7 8 co = coroutine.create( -- 创建co协程 9 function(a,b) 10 print("into co 1,a = "..a..",b = "..b) 11 local r = testFun(a) 12 print("into co 2,r = "..r..",a = "..a..",b = "..b) 13 local m,n = coroutine.yield(2 * a , 2 * b) -- 挂起co协程 14 print("into co 3,m = "..m..",n = "..n..",a = "..a..",b = "..b) 15 return a,b,"结束co协程" 16 end 17 ) 18 19 print("main",coroutine.resume(co,10,20)) -- 启动co协程 20 print("main",coroutine.resume(co,"resume1")) -- 唤醒co协程 21 print("main",coroutine.resume(co,"resume2-m","resume2-n")) -- 唤醒co协程 22 print("main",coroutine.status(co)) 23 24 运行结果: 25 Lua 协程测试2 26 into co 1,a = 10,b = 20 27 into foo,n = 10 28 main true 100 29 into co 2,r = resume1,a = 10,b = 20 30 main true 20 40 31 into co 3,m = resume2-m,n = resume2-n,a = 10,b = 20 32 main true 10 20 结束co协程 33 main dead
|
请发表评论