Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Purpose to call an IJob from OnUpdate and to call Complete after schedule

Discussion in 'Entity Component System' started by Sylmerria, Nov 30, 2018.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi guys,

    I was watching the code source of MeshInstanceRendererSystem and many times in there an IJob is created from OnUpdate but the system wait the end of this job by calling Complete.

    Doesn't calling Complete method blocks the main thread ? So what's the purpose of that ?

    Thanks
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Correct, Complete does block the main thread. I believe the purpose is to allow burst compiled code to run, since burst is only compatible with jobs.
     
    eizenhorn likes this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    It not depend on Burst or not, it's manual synch point when you need job results here immediatley, and it bloks main thread because you say: "Hey Job! I need your results here, on main thread, I want use thes results" thus Main thread must wait this result before processing.
     
    dartriminis likes this.
  4. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    That too. I guess I was thinking more along the lines of: "Why even bother coding a job (as opposed to putting that code in the Update method) if you are just going to immediately complete it."
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    I'm sorry a little misunderstood your message :) I re-read it and understood what you meant - they use the job as a function, but due to Burst It will be executed many times faster than a similar function on the main thread. It's correct opinion :) Yes they do this for that reason :) Just the first time I thought you wrote it in the meaning - "Complete() is needed for the work of Burst code", that's why I wrote the previous message
     
    dartriminis likes this.
  6. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Using IJob.Schedule().Complete() is basically guaranteed to run in the main thread as the job was not yet scheduled for execution. If the IJob had already been scheduled (eg using ScheduleBatchedJobs) and started on a different thread the main thread would be blocked, that would be bad.

    In addition MeshInstanceRendererSystem uses IJobParallelFor in the chain that is 'Complete'ed -> this is using multiple cores.
     
  7. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Thanks guys for the clarification :)

    @julian-moschuering By the way, great job with dungeons series. I love it ;)
     
    julian-moschuering likes this.