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.

2 Likes