Search code examples
multithreadinglua

embedding multiple lua instances in a multiple threaded program


I have a program with 4 threads.

Within each thread, I do a luaL_newstate();

Each thread only has access to it's own lua instance.

Is there anything I need to worry about? [I.e. is there some hidden state that all lua instances share behind my back?]

Thanks!


Solution

  • No, that should work just fine. All interpreter state is self contained in each Lua instance. I would even say that is the preferred way to use Lua with multiple threads and/or processes.

    If you find that you do need to communicate between Lua states eventually, then it is best to serialize the data and pass it using the C API. I recommend reading the "Exploring Lua for Concurrent Programming" whitepaper. It introduces a method of using multiple Lua processes with message passing for inter-process communication.