Lua_newstate vs lua_newthread

I am trying to implement lua in my existing multithreaded application. I heard that lua was not thread safe. I created different lua_State(s) for different threads.

Looking through the lua header files, I found lua_newthread . How would you implement this into an entire threaded application. Do you create one lua_State and create a separate lua_newthread(s) or will this lead to other problems?

+5
source share
2 answers

Beware, Lua threads do not work in system threads (despite the misleading name), only objects in Lua itself (and not in the host application) to execute the code asynchronously.

So the answer is: create one lua_State for the stream in your application and use the serialization library as middleware if you need to pass data between states.

+6
source

lua_newstate creates new Lua states. The various states are completely separate.

lua_newthread creates a new Lua thread attached to this Lua state. Lua state can have multiple threads of execution inside the Lua virtual machine, but they are not executed at the same time; they are coroutines and can exchange data.

Do not confuse Lua threads with operating system threads.

+8
source

Source: https://habr.com/ru/post/1205547/


All Articles