Although the Lua system as a whole is fully re-entrant, individual lua_State
instances are not in themselves thread safe.
If you're accessing a lua_State
from multiple threads, you should use a mutex or other locking mechanism to ensure that only one thread at a time can manipulate that state. Simultaneous accesses could easily result in the sort of corruption you're seeing.
If you're working with multiple lua_State
instances, each state can have its own access lock; you don't need a single global lock for the whole Lua runtime.