multithreading – Lua registry not visible from new states – Education Career Blog

In a C function called from my Lua script, I’m using luaL_ref to store a reference to a function. However, if I then try to use the returned integer index to fetch that function from a different thread which isn’t derived from the same state, all I get back is nil. Here’s the simplest example that seems to demonstrate it:

// Assumes a valid lua_State pL, with a function on top of the stack
int nFunctionRef = luaL_ref(pL, LUA_REGISTRYINDEX);

// Create a new state
lua_State* pL2 = luaL_newstate();
lua_rawgeti(pL2, LUA_REGISTRYINDEX, nFunctionRef);
const char* szType = luaL_typename(pL2, -1);

I’m finding that szType then contains the value ‘nil’.

My understanding was that the registry was globally shared between all C code, so can anyone explain why this doesn’t work?

If the registry isn’t globally shared in that way, how can I get access to my values like I need to from another script?


The registry is just a normal table in a Lua state, therefore two unrelated Lua states can’t access the same registry.

As Kknd says, you’ll have to provide your own mechanism. A common trick is creating an extra state that doesn’t execute any code, it’s used only as a storage. In your case, you’d use that extra state’s registry from your C code. unfortunately, there’s no available method to copy arbitrary values between two states, so you’ll have to unroll any tables.

copying functions is especially hard, if you’re using the registry for that, you might want to keep track of which state you used to store it, and execute it on the original state, effectively turning it into a cross-state call, instead of moving the function.


luaL_newstate() creates another separeted state, as the name says. The registry is only shared between ‘threads’, created with lua_newthread(parent_state);

Edit to match the question edit:

You can run the scripts in the same state, or, if you don’t want that, you will need to provide your own
mechanism to synchronize the data between the two states.


To use multiple Lua universes (states) you might find Lua Lanes worth a look. There is also a rough comparison of multi-state Lua solutions.

Lanes actually does provide the ‘hidden state’ that Javier mentions. It also handles locks needed to access such shared data and the ability to wait for such data to change. And it copies anything that is copyable (including functions and closures) between your application states and the hidden state.

Leave a Comment