Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

Bug Job Execute function running on Schedule() instead of Complete()

Discussion in 'Data Oriented Technology Stack' started by slushieboy99, May 20, 2021.

  1. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    31
    Hello,
    I schedule my jobs in Update() and send the JobHandlers to a queue of JobHandlers. This queue is iterated over in LateUpdate and the jobs are completed. After a lot of debugging, I've found that my jobs are being run during Update, not LateUpdate, specifically when they are scheduled. I am only running Complete() in one area of my code, and am finding that jobs' execute functions are being run before this is ever called.

    I can't for the life of me figure out what is causing this, has anyone run into this problem before?

    EDIT: When I enable Show Timings this no longer happens, but my frame time quadruples and I get 50 errors that are not viewable in the console. This is very peculiar, I'm changing flair to bug.
     
    Last edited: May 20, 2021
  2. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    700
    Complete() doesn't run jobs. It only guarantees that they finished. This means only jobs which have not yet been executed get executed on Complete().
    Jobs are executed at some point between Schedule() and Complete(). That can be sooner or later.

    What you are experiencing is how it should be btw. If calling Complete() runs your job, that means that the job hasn't been finished yet. This is bad, because it means your main thread gets blocked while the job is executed. Calling Complete() on an already finished job is basically free.
     
  3. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    31
    Ah, I see. I don't know why I didn't know it worked this way. So Schedule() will allow the job to start at any point?
     
  4. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    725
  5. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I have spent quite a bit of effort making boilerplates for this to ensure the proper procedures with regards to data lifespan, read/write access and so on and so forth...
    My most common approach so far is to schedule a Job early in a frame with two if-checks, one if it has been scheduled and another if it IsCompleted() (which will be on the next frame at the earliest), then of course there's the Job-dependencies themselves and especially now that I have a series of Jobs operating in a sequence that can potentially be shortcut.

    It's not fun, and I think (and hope) this might change.
     
unityunity