lua-users home
lua-l archive

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


Thanks for your response. So to make the thread garbage collected I have to search the lua state's stack and remove the thread object from it so that ican get garbage collected?
       For the environment tables if I don't store them in the global state then to access any values from them myself I have to search for the thread in the stack, get its environment and then get values from it?

Thanks,
Milind


On Sun, Mar 30, 2014 at 11:00 PM, William Ahern <william@25thandclement.com> wrote:
On Sun, Mar 30, 2014 at 10:14:20PM -0700, Milind Gupta wrote:
>
> Hi,
>
> After I create a lua thread in C how do I some point destroy it or have it
> garbage collected? I see that lua_close does not work on threads.

Lua threads are objects just like any other. You don't need to do anything
special for them to be garbage collected. Once there are no more references
to a coroutine--from the stack of the main thread, from some global, or from
the registry--it becomes garbage.

You can think of coroutines as similar to userdata objects. You can't store
a pointer to a userdata object outside of Lua's view and expect it to remain
valid. If Lua can't find the reference, it will mark it as garbage and
destroy it, invalidating the pointer.

> Another question is that if I want to have multiple threads in 1 lua
> state with each thread running in its own environment. I suppose I have
> to store all the environment tables in the state's global variable so
> that it does not get garbage collected?

Nope. And the reason relates back to the previous answer. What matters is
that your coroutine is anchored to the global state somehow. Once it's no
longer anchored, it's garbage.

> If that is the case and if the environment table has a metatable with
> __index=_G so the thread can access the main state's global space then the
> thread would be able to modify their own environments. How can I keep the
> environment tables isolated from those threads and still allow access to
> the Global variables? Thanks, Milind

See above.

Cheers,

- Bill