Job stalls the main thread

I’m scheduling a bunch of IJobs via Schedule method. I expect them to run on the worker threads, not blocking the main thread.

But somehow, each frame, URP’s render loop waits for one of those jobs to complete, while other scheduled jobs run in the background as expected.

I’m completely clueless as to why this is happening. There are no job dependencies associated with that job.

Could you give me a hint? Why this is happening?

You should not schedule long-running jobs in parallel to use all threads. Otherwise, there won’t be any threads free to do the jobs the engine itself schedules. It is a little suboptimal on Unity’s part that the main thread is picking up one of the long-running jobs and not a job in the dependency graph it is actually waiting on, but the behavior varies between different versions.

1 Like

Wow, this behaviour makes zero sense, I’m still completely clueless as to why this is happening.

So you’re saying that freeing up a thread for Unity will help? How can I do this?

Yes. And you do it by limiting the number of jobs you schedule to be less than the worker thread count (which you can get from JobsUtility).

1 Like

I see. Thank you!