Question

How do I write a thread, or a co-routine (correct my nomenclature) to execute one function in Lua while the mainline code looks at its results and makes decisions ?

What I want is a function, something like

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

I would then like to have a main line code do something like

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

What's the syntax ? Where do I read about this ?

What else am I missing ?

Was it helpful?

Solution

lua co-routines are collaboratively threaded. They need to explicitly yield control to one another. They don't run at the same time.

If you need preemptive threading you need one of the various lua threading libraries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top