lua-users home
lua-l archive

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


On Fri, Nov 25, 2011 at 2:45 AM, HyperHacker <hyperhacker@gmail.com> wrote:
> On Thu, Nov 24, 2011 at 03:57, Duncan Cross <duncan.cross@gmail.com> wrote:
>> On Wed, Nov 23, 2011 at 11:16 PM, Martin Linklater
>> <mslinklater@gmail.com> wrote:
>>> Thanks. So after creating the new thread, stack position -1 contains a thread object. When you say to keep a reference to it do I need to create a Lua table entry which contains that thread object ? Or is there a simpler way ?
>>>
>>> Cheers
>>
>> What I would do is use the registry table, and create an entry where
>> the key is a light-userdata pointer to the thread. Something like
>> this:
>>
>>  // thread object is on the top of L's stack
>>  lua_pushlightuserdata(L, lua_topointer(L, -1));
>>  lua_pushvalue(L, -2);
>>  lua_settable(L, LUA_REGISTRYINDEX);
>>
>> Later, if you want to remove this entry from the registry table and
>> make the thread garbage-collectable again, push another light userdata
>> and set its registry table entry to nil.
>>
>> -Duncan
>>
>
> Er, why pushing pointers to the objects instead of the thread itself?
>
> --
> Sent from my toaster.
>
>

Well, you can't push an arbitrary thread object directly onto a sister
lua_State's stack. There's lua_pushthread(), but that only takes one
parameter - it pushes a thread's object onto its *own* stack. It's
possible that's okay, but I couldn't remember whether it is
inadvisable to do things on a thread state's stack if it might be a
dead coroutine or something, so using the pointer seemed safer. There
might be no problem.

-Duncan