lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Apr 9, 2011 at 4:35 PM, Anthony Howe <achowe+lua@snert.com> wrote:
> Is it possible somehow to reset a Lua coroutine thread in the event of a
> script error or some such, without having to nuke the thread and
> recreate the thread's state?

You can't recycle a thread that died due to an error. You can recycle
threads that end for normal reasons by using a main function that
accepts new functions to run. Something like:

local function run (f, ...)
  return coroutine.yield(f(...))
end

function thread_main ()
  local DONE = {} -- unique value to signal finished running a thread
  while true do
    run(coroutine.yield(DONE)) -- give DONE to caller, we want
something new to run
  end
end

In 5.2 you can use pcall f in the run function above so that you can
recover even from errors.

-- 
- Patrick Donnelly