lua-users home
lua-l archive

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


On Saturday 09, Anthony Howe 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?

I think something like this would work.  (Warning un-tested code).

function resettable_thread(func)
  return coroutine.create(function()
    repeat
      coroutine.yield(pcall(func))
    until false
  end
end

It will always restart the thread with the same function.  You could use a 
more complex wrapper to allow changing the function that the thread runs.

-- 
Robert G. Jakabosky