Order of execution when desynchronizing (Parallel Lua)

I’m trying to use parallel Lua for a bit of computing each frame but I’m honestly not too sure what exactly happens when you desynchronize a task. Right now I made sure that the code I want to be parallel solely crunches numbers (no instance access)

It follows this kind of format but I get random weird errors and my debug.profileend/begin calls seem to be out of order because I get spammed with warnings:

runservice.Stepped:Connect(function() -- (1)
  do a couple of things -- (2)
  
  task.desynchronize() -- (3)
  debug.profilebegin("StepCalc")

  EXPENSIVE FOR LOOP -- (4)

  debug.profileend()
  task.synchronize() -- (5)
  
  reflect the computed data on the main thread -- (6)
end)

What’s the order of execution here? I added some numbers so they can be referred to as steps 1-6. I understand that parallel execution isn’t linear, I’m just looking for some sort of explanation as to what is happening if I try to run this code every frame. Thanks.

3 Likes

in this case, the order of execution works exactly the same
if there are multiple scripts in parallel doing (4) at the same time however, all of them will run together at the same time, order kinda depends on which one finishes first, you can consider them doing it all at the same time

so basically

-- during "___", it's not running this scripts code. It may either run other script's code or do nothing.
main thread : (1) -> (2) -> (3) -> ___ -> ___ -> (6)
other thread: ___ -> ___ -> ___ -> (4) -> (5) -> ___

you can also use microprofiler to see for yourself, press F6 to toggle, then press P to pause the profiler and check info about script execution, press P again to unpause

1 Like