Are actors *true* multithreading? Or is there something wrong with my script

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

  1. Create two Actors in ServerScriptService. Call one “HangActor” and the other “ParallelActor”
  2. 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()
  1. 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()
  1. 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!

This section seems to explain the reason.

Unless someone else can chime in and explain another reason why, I’ll just mark this as solved.

1 Like

That’s the correct reason, just important to remember that there are still steps to rendering. And with Actors being a new and powerful features means stalling a thread has little to no safeguards so bad code can result in really really bad things.

Since the game waits for parallel code to finish and you are telling the parallel code to do nothing forever it will be waiting for nothing forever to finish before it continues rendering. Normally luau has a safeguard to exit high-cpu nothing forevers but parallel does not and now Windows has to kill your studio client.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.