lua-users home
lua-l archive

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


I need to store a tidbit of C data on the ?Lua side? for each thread I
create with lua_newthread().  Specifically, I am storing a pointer to the
C++ object that manages that thread.  From the documentation it looks like
each thread gets a COPY of the parent thread?s global environment table, so
this seemed like the place to store my data.  With each new thread I create,
I do something like this:

    // save the object managing this thread
    lua_pushlightuserdata(l, this);
    lua_setglobal(l, "LuaScriptObject");

Then, when my C functions get called from Lua, I grab the address of the C++
object I saved like this:

    // grab the object managing this script
    lua_getglobal(l, "LuaScript");
    managerObject = (cast)lua_touserdata(l, -1);

The problem comes in when I have one thread launch another thread.  When the
child thread dies, any C functions called from the parent thread still seem
to be getting the child thread?s manager object ? as if the global
environment table is SHARED across threads.

Any ideas?