lua 代码有什么问题?

local consumer = coroutine.create(function(i)
   while true do
    i = i - 1
    print('consumer: ' .. i)
    coroutine.yield(i)
   end
end)

local producer = coroutine.create(function()
   while true do
     i = i or 0
     i = i + 1
     print('producer: ' .. i)
     status, value = coroutine.resume(consumer, i)
     i = value
   end
end)

coroutine.resume(producer)

输出结果如下

但实际结果完全不同,正确的输出应该是: 0 1 0 1 01

点赞
用户7832012
用户7832012

在消费者中将 coroutine.yield(i) 替换为 i = coroutine.yield(i)。 @EgorSkriptunoff 感谢您的帮助

2020-06-22 04:48:04