Hi!
With the announcement of Actors and parallel Luau, I got very excited at the possibilities of multithreading.
Just to be on the same page, multithreading is about running code simultaneously without needing to yield in order for the other thread to continue. Theoretically, even a while true do end
thread would not cause all the others to hang. That’s why if you compiled a C program with while (true) {}
the rest of the OS doesn’t crash.
But when I tried this experiment in the Studio today, it seems like it isn’t the case. Please correct any/all of my mistakes that I have made.
Setup/Steps to reproduce
- Create two Actors in ServerScriptService. Call one “HangActor” and the other “ParallelActor”
- In “HangActor”, create a Script with the following code:
task.desynchronize()
print("Pre hang loop")
while true do
end
print("Post hang loop")
task.synchronize()
- In “ParallelActor”, create a Script with the following code:
task.desynchronize()
local initTime = os.time()
while true do
local timePassed = os.time() - initTime
task.wait()
print(`Running for {timePassed} seconds.`)
if timePassed > 15 then
return
end
end
task.synchronize()
- Run!
You may notice that other parts of the Studio don’t hang because the code is running under a separate thread, and you can use other parts of the Studio without waiting for the first Actor to crash…
What I would expect to see is that “ParallelActor” would continue running despite “HangActor” freezing everything as they run under separate threads and HangActor would only hang its own thread. What we see instead is that they both freeze. In fact, rendering also freezes and I’m pretty sure Physics does as well.
Is there something wrong with my experiment, or is this just a limitation of parallel Luau? If it is the latter, then to what extents does it run stuff in parallel? Is it limited towards loops? Is it limited to setting values? Is it limited to inserting stuff into tables? What constitutes parallel code?
Thank you for your time!