Job System scheduling - general question

I am investigating DOTS and I am looking for confirmation that I understood this part correctly.

when I call .Schedule() on a job it gets added to the queue.
but only when I call handle.Complete() the Job System may start execution.

so given:

10 j = new MyJob{…}
11 JobHandle h = j.Schedule();
12 h.Complete()
13 //use Data from job

all the job-work is guaranteed to happen between line 12 and 13.
Complete() waits for the job to finish and only after that, line 13 gets executed

so if I would write:

h.Complete()
h1.Complete()
h2.Complete()

that would be very inefficient because each line would wait until its job returned done.

so to handle this, use:

JobHandle.CompleteAll(h, h1, h2)
to let the jobSystem optimize the order and use all cores

IJobParallelFor()
to let the Job System split one job to all available cores.

Is this correct?
Any help and further information is very much appreciated!

You are correct. You can also use JobHandle.ScheduleBatchedJobs to flush the queue without forcing completion. That lets you get the first jobs started while you tack onto the chain.

1 Like

@DreamingImLatios thank you for your reply!
May I ask another question?

with the following code:

NativeArray ents = new NativeArray(10, Allocator.Persistent);
m.CreateEntity(archetpye, ents);
ents.Dispose();

you allocate an array and during CreateEntity() the entities get created in the array and the immediately transferred into their chunks.
you dont need the array anymore and can dispose it.

why would you not have something like this:

m.CreateEntity(archetpye, 10);

where unity hides the creation/destruction of the nativeArray from you?

No idea.

But you can do this and ignore the return result:
m.CreateEntity(archetype, 10, Allocator.Temp);

1 Like