Lua协程-测试3

print("Lua 协程测试3")

-- 实现消费者-生产者关系(生产一个就消费一个)
count = 10 -- 生产总数

-- 生产者
local newProductorCo = coroutine.create( --创建协程
    function()
      local i = 1
      while(i <= count)do
          coroutine.yield(i) -- 挂起协程
          i = i + 1
      end
    end)

-- 消费者
for i=1,count do
    local status,value = coroutine.resume(newProductorCo)
    print(status,value)
end

运行结果:
Lua 协程测试3
true    1
true    2
true    3
true    4
true    5
true    6
true    7
true    8
true    9
true    10