Lua计时线程

时间:2014-05-15 16:52:02

标签: multithreading lua

当主线代码查看结果并做出决定时,如何编写线程或协同例程(更正我的命名法)以在Lua中执行一个函数?

我想要的是一个功能,比如

-----------------------------------------------------------
--   Countdown_Nine_Seconds()                            --
--                                                       --
--   On Entry:  Nothing                                  --
--                                                       --
--   Returns:   Nothing                                  --
--                                                       --
--   Action:    Decrements these two counters...         --
--                                                       --
--              Full_Wait                                --
--              Tenth_Wait                               --
-----------------------------------------------------------

  function Countdown_Ten_Seconds()

  Full_Wait = 9                             -- This will be a global
  local i
  local j

  for i = 9, 0, -1
    do                                      -- This is the I loop start
     Tenth_Wait = 10                        -- ten tenths in a second
      for j = 10, 0, -1
       do                                   -- This is the J loop start
        box.wait(100)                       -- our implementation has this; wait 0.1 seconds
         Tenth_Wait = Tenth_Wait - 1        -- Tell the rest of the world
          end                               -- end of inner J-Loop
           Full_Wait = Full_Wait - 1        -- One less second
            end                             -- end of ouer I-Loop
             end                            -- end of this complete function

我希望主线代码可以像

那样
  While(Full_Wait > 0)
     do
     :
     :
     :
     (my stuff here)
     :
     :
     :
     :
     end      

语法是什么?我在哪里读到这个?

我还缺少什么?

1 个答案:

答案 0 :(得分:2)

lua协同例程是协作线程。他们需要明确地相互控制。他们不会在同一时间跑。

如果您需要抢占式线程,则需要使用各种lua线程库之一。

相关问题