我的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

青松周

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章