How should you set up threading for Luau? #311
-
I may have a misunderstanding here, but I am under the notion that Luau is used in a threaded environment for Roblox? I was curious about how an embedding application would correctly setup a system similar to this. And what sort of caveats I might run into. Is it that the main state, L is to exist on the application's main thread. Then for each worker thread to run lua code, you use the c function lua_newthread (or Luau's equivalent), to create the child state l. Afterwards, the GC is expected to be ran from the main thread at the application's disgression. Is this the correct method, or am I missing something here. Finally. How does Luau manage data between threads safely? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Luau currently requires the host to ensure exclusive access to different VMs (aka top-level In Roblox, we create many VMs and load individual scripts into separate VMs; since we expose some host objects to all VMs via bindings, we can provide a way for threads to communicate by unmarshalling the Luau objects (converting them to C++ state) from one VM, sending that via a queue to a different VM, and marshalling them back (converting from C++ state). We are likely going to experiment more in this area in the future but that's the status quo. |
Beta Was this translation helpful? Give feedback.
Luau currently requires the host to ensure exclusive access to different VMs (aka top-level
lua_State
), which is the same requirement that Lua has. Separate coroutines that belong to the same state can not be executed or worked with concurrently, as internal components of the VM such as the garbage collector are not thread-safe. The term thread inlua_newthread
refers to the thread of execution within the VM (better known as coroutine), and should not be confused with hardware threads.In Roblox, we create many VMs and load individual scripts into separate VMs; since we expose some host objects to all VMs via bindings, we can provide a way for threads to communicate by unmarshalling the L…