Synchronizing in Parallel Lua Question

Hi

So I have a Question about Parallel Luau based on its usage of synchronizing the code between Parallel and Serial Luau as I do not exactly understand when and how to use the task functions properly, unless I already am.

Although I understand of what the usage of task.synchronize and task.desynchronize does, I am wondering about the constant usage of task.synchronize where I keep switching from Parallel to Serial.

Like for Example, I have this code that runs in an Actor under StarterPlayerScripts where when you die, Tweening of some sorts happens.

task.synchronize() -- runs code in Serial Luau
Humanoid.Died:ConnectParallel(function() -- Connects Event in Parallel
    task.synchronize() -- runs code in Serial Luau because thread safety reasons
    TweenSvc:Create(Item, Info, Goal):Play() -- Tweens
end)
task.desynchronize() -- runs code in Parallel Luau

If I keep synchronizing this for everything inside this script, should I even be using Parallel Luau for this?
Does this make the code more performant than without it?

:ConnectParallel spawns a completely new thread for the callback every time the signal is fired, so by switching back to serial like this you’re effectively doing the same as:

while true do
	task.desynchronize()
	Humanoid.Died:Wait()
	task.spawn(function()
		task.synchronize()
		...
	end)
end

So, there’s no point in using Parallelism for something as trivial as tweening something here.

Infact, there’s not much point in using it really in anything currently because of how bottlenecked the API is. Until we get dedicated task scheduling for multi-threading functions, the cost of going in and out of Parallel just outweighs the speed of computing on multiple hardware.

4 Likes

I see, I guess that makes sense, thanks.

Exactly yeah, parallel LUAU is intended to be used for calculating and returning processed data. Handling states is the serial/main thread’s job. Though Parallel API is bottlenecked, I still found some uses with this API.

1 Like

Im mainly handling all the Info, and Calculations in Parallel, its just the Tweening thats in Serial.

My Question was if i was using task.synchronize that much, was there a point in using Parallel Lua?

1 Like

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