Is there a better way to force the jobs scheduler to start?

I have a job related issue that I’ve found an ugly solution for, but suspect there is a better way.

I have a lengthy task that has to run on the main thread every frame (lengthy = 3-5ms). I also have a lot of computational work for which I use jobs, which take a similar amount of time in total.

Obviously I want my lengthy main-thread task to run in parallel to the jobs, so at the start of the frame (from FixedUpdate if there is one, otherwise Update) I schedule the jobs, then start the lengthy task. The jobs and the lengthy task have no interdependencies. I call complete on the jobs at the end of the frame, from a late update.

The problem is that according to the profiler, the jobs do not start until the lengthy task completes. It appears that the job scheduler is running on the main thread, and hence isn’t able to do it’s stuff until I release the main thread again - which I can’t really do until the lengthy task finishes. For unrelated reasons, I don’t want to run the lengthy task at the start of late update rather than the end of the early Update/FixedUpdate (though this would of course solve the problem).

What I really want to do is force the scheduler to run and start my jobs as soon as I’ve scheduled them. I’d hoped there would be an API call to do this, but I can’t find one. Am I missing something? I’ve discovered I can force this by scheduling a dummy do-nothing job in my early Update/FixedUpdate (after I schedule the real jobs) and calling complete on that as soon as I’ve scheduled it, and before I start my lengthy task. This works (i.e. the complete seems to make the scheduler do it’s stuff), but is obviously a kludge.

Is there a better way?

ScheduleBatchedJobs is what you’re looking for

2 Likes

Thanks! Not sure how I failed to find that, but yes, precisely what I was looking for.