lua-users home
lua-l archive

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


Graham Wakefield wrote:
(reposted with subject, sorry)

On Apr 2, 2007, at 2:24 AM, Graham Wakefield wrote:

Hi all,

I would like to implement a 'thread pool' of Lua states (allocated with lua_newthread); what do I need to do to an existing lua State to clean it up before returning it to the pool?

I figured at least lua_settop(L, 0), but what googling I have done also mentions that I should be careful about exceptions/errors in a state; but no more information could be found. What do I need to do?

If you've executed a lua_resume on the thread, I think it's only safe
to reuse the thread after the thread's function has returned. Otherwise,
you might not properly close open upvalues. You probably should not
attempt to reuse a thread which has thrown an error.

At least, that's the strategy I've used: if the thread has returned
normally, I put it back into the pool; otherwise, I let it get
garbage collected. In almost all cases the thread returns normally,
so it's not worth spending a lot of time worrying about corner
cases. If the thread returns normally, then there should be
nothing on the stack, unless you've asked for return values in
which case you need to pop or xmove them.

You cannot "reparent" a thread.