Lua协程还有什么用?为什么此代码无法按我预期的那样工作? [英] What are Lua coroutines even for? Why doesn't this code work as I expect it?

查看:334
本文介绍了Lua协程还有什么用?为什么此代码无法按我预期的那样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解这段代码时遇到了麻烦...我期待与线程类似的东西,因为它们都是异步进行打印的,所以我的输出会出现随机的"nooo"和"yaaaay"相互穿插的情况,但是而是我发现主线程似乎在第一次调用coroutine.resume()时阻塞了,因此阻止了下一个线程的启动直到第一个线程退出为止.

I'm having trouble understanding this code... I was expecting something similar to threading where I would get an output with random "nooo" and "yaaaay"s interspersed with each other as they both do the printing asynchronously, but rather I discovered that the main thread seems to block on the first calling of coroutine.resume() and thus prevents the next from being started until the first has yielded.

如果这是预定的操作协程,它们有什么用,我将如何实现我希望的目标?我是否需要实现自己的调度程序以使这些协程程序异步运行?因为这看起来很杂乱,所以我不妨使用函数!

If this is the intended operation coroutines, what are they useful for, and how would I achieve the goal I was hoping for? Would I have to implement my own scheduler for these coroutines to operate asynchronously?, because that seems messy, and I may as well use functions!

co1 = coroutine.create(function ()
        local i = 1
        while i < 200 do
                print("nooo")
                i = i + 1
        end
        coroutine.yield()
end)

co2 = coroutine.create(function ()
        local i = 1
        while i < 200 do
                print("yaaaay")
                i = i + 1
        end
        coroutine.yield()
end)

coroutine.resume(co1)
coroutine.resume(co2)

推荐答案

协程不是线程.

协程就像永远不会主动调度的线程一样.因此,是的,您是正确的,您必须编写自己的调度程序才能使两个协程同时运行.

Coroutines are like threads that are never actively scheduled. So yes you are kinda correct that you would have to write you own scheduler to have both coroutines run simultaneously.

但是,在协程方面,您却错过了更大的前景.查看Wikipedia的协程使用列表.这是一个具体的示例,可能会指导您正确的方向.

However you are missing the bigger picture when it comes to coroutines. Check out wikipedia's list of coroutine uses. Here is one concrete example that might guide you in the right direction.

-- level script
-- a volcano erupts every 2 minutes
function level_with_volcano( interface )

   while true do
      wait(seconds(5))
      start_eruption_volcano()
      wait(frames(10))
      s = play("rumble_sound")
      wait( end_of(s) )
      start_camera_shake()

      -- more stuff

      wait(minutes(2))
    end


end

上面的脚本可以编写为使用switch语句和一些巧妙的状态变量来迭代运行.但是,当以协程书写时,这一点要清楚得多.上面的脚本可能是一个线程,但是您是否真的需要将内核线程专用于此简单代码.繁忙的游戏关卡可以在不影响性能的情况下运行100个协程.但是,如果每个线程都是一个线程,您可能在性能开始下降之前就离开20-30.

The above script could be written to run iteratively with a switch statement and some clever state variables. But it is much more clear when written as a coroutine. The above script could be a thread but do you really need to dedicate a kernel thread to this simple code. A busy game level could have 100's of these coroutines running without impacting performance. However if each of these were a thread you might get away with 20-30 before performance started to suffer.

协程旨在让我编写将状态存储在堆栈上的代码,这样我就可以停止运行它一段时间(wait函数),然后从我中断的地方重新启动.

A coroutine is meant to allow me to write code that stores state on the stack so that I can stop running it for a while (the wait functions) and start it again where I left off.

这篇关于Lua协程还有什么用?为什么此代码无法按我预期的那样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆