Search Unity

Question Limit ParallelFor Job num Workers/Threads

Discussion in 'Entity Component System' started by zixuan313, Jun 12, 2022.

  1. zixuan313

    zixuan313

    Joined:
    Nov 22, 2021
    Posts:
    2
    Is there a way we can limit the number of workers/threads of a ParallelFor job? It seems to use all available workers by default.

    The main thread might wait for job completion, and execute some rendering jobs before this frame would finish(I did not call jobhandle.Complete()).

    I tried to set JobsUtility.JobWorkerCount, but that does not work and I lost one of my workers :(.
    upload_2022-6-13_4-46-0.png
     
    SuJm and hiarthurji like this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    I don‘t understand what you want to achieve and what you mean by main thread waiting for completion?

    If a job takes a long time, let‘s say one second, it‘s not going to affect the main thread in any way - unless you call JobHandle.Complete() at which point the main thread is blocked and the next frame will be rendered after about one second.

    If you want a long-running background task on one thread only you can use a regular IJob with a loop. Or several, each getting a slice of the array they should work on.
     
    MNNoxMortem likes this.
  3. zixuan313

    zixuan313

    Joined:
    Nov 22, 2021
    Posts:
    2
    Thanks for your advice! This was what I expected, and everything worked fine with non-ParallelFor jobs. However, it seems some rendering jobs must be done by job workers. When all job workers are busy working on a ParallelFor job, the main thread has to wait for its completion.

    I am wondering if we can limit the number of worker used by a ParallelFor job, and reserve some workers for other system jobs(rendering, etc.).
     
    hiarthurji likes this.