lua-users home
lua-l archive

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



> So, is it safe to do the following:

> // creating thread
> m_thread               = lua_newthread(L);
> m_thread_reference     = luaL_ref(L,-1);
> lua_pop                (L,1);

> // killing finished or yielded thread
> lua_pushlightuserdata  (L,m_thread);
> luaL_unref             (L,-1,m_thread_reference);
> lua_pop                (L,1);

Not quite right. luaL_ref takes a table index as its second argument; that is the table in which the references are stored, and is the same table which luaL_unref must be given as a second argument. Typically LUA_REGISTRYINDEX is used as the table index, but you could use a table of your own.

luaL_ref(L, t) pops the top stack object and returns a reference index (which is an integer key into the table at t).
luaL_unref(L, t, idx) deletes the object with index idx from the table at t. It does not change the stack.

So:

// Creating thread
m_thread           = lua_newthread(L);
m_thread_reference = luaL_ref(L, LUA_REGISTRYINDEX);

// Releasing thread, whether finished or yielded
luaL_unref(L, LUA_REGISTRYINDEX, m_thread_reference);