(7)Lua 协程

  1.创建并运行

(1)方法1

local r=coroutine.create( --创建
    function (a,b)
        print(a+b)
        print('end')
    end
)
coroutine.resume(r,3,5) --执行

(2)方法2

local r=coroutine.wrap( --创建
    function (a,b)
        print(a+b)
        print('end')
    end
)
r(3,5) --执行

2.挂起 coroutine

这种方法只返回一个值

local r=coroutine.wrap(
    function (a,b)
        print(a+b)
        coroutine.yield() --下次调用从这里执行
        print('end')
    end
)
r(3,5)
r()

3.返回值

(1)create创建的线程多返回一个布尔值

local i,j=coroutine.resume(r,3,5)
print(i,j)

(2) 使用这种方法直接返回

--
coroutine.wrap
原文地址:https://www.cnblogs.com/buchizaodian/p/12509417.html