lua-users home
lua-l archive

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


> I was looking for a "coroutine.reset" function, in order
> to be able to reuse a thread or to be able to "restart"
> it from the beginning.
> 
> [...]
> 
> What do people from the Lua team think of the idea ?

Sometimes we fancy the idea of such reset function (not to restart a
thread, but to clear it completely to be reused). But I think you can
have almost the same result by writing the thread main function as a
loop, more or less like this:

co = coroutine.wrap(function (f)
       repeat
         local result = f()   -- do required computation
         -- yields last result and waits for new computation
         f = coroutine.yield(result)
       until f == nil
     end

-- Roberto