[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: thread-specific data storage
- From: "Kevin Baca" <lualist@...>
- Date: Mon, 1 Dec 2003 15:56:04 -0800
Each new thread's global table reference points to the *same* table as
the spawning thread's global table reference.
If you want a thread to have a unique globals table, use lua_replace():
lua_newtable( L );
//Table setup code omitted.
lua_replace( L, LUA_GLOBALSINDEX );
If you simply want to associate thread specific data (TSD) with each
thread:
lua_State* new_L = lua_newthread( L );
lua_pushlightuserdata( L, new_L );
lua_pushlightuserdata( L, this )
lua_settable( L, LUA_GLOBALSINDEX );
To retrieve the TSD:
lua_pushlightuserdata( L, L );
lua_gettable( L, LUA_GLOBALSINDEX );
-Kevin
>
>
> 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
> C++ 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?
>