从文档中,我了解到创建的新线程必须在使用前正确锚定。为此,我想在注册表中保留对新线程的引用,(Table[thread-addr] = thread
)为此,我正在这样做:
lua_State *L = NULL;
lua_State *L1 = NULL;
int tref = LUA_NOREF;
L = luaL_newstate(); // main lua thread/state
// create a table in registry: Table[thr-addr] = Thread
lua_newtable(L);
tref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
L1 = lua_newthread(L);
// Anchor it
lua_rawgeti(L, LUA_REGISTRYINDEX, tref);
lua_pushnumber(L, (ptrdiff_t) L1);
lua_pushlightuserdata(L, L1);
lua_settable(L, -3);
完成线程后,我计划设置这Table[thread-addr] = nil
是否足够?或者我也应该为它设置一个元表,带有弱键/值?
谢谢。