JobHandle and dependencies
Job의 Schedule() 메서드를 호출하면 JobHandle을 반환한다. 이것으로 다른 Job들과 의존성으로 사용할수 있다. 한개의 Job이 다른 Job의 결과에 의존적이라면, 첫번째 Job의 JobHandle을 두번째 Job의 Schedule 메서드에 변수로 넣는다.
JobHandle firstJobHandle = firstJob.Schedule();
secondJob.Schedule(firstJobHandle);
Combining dependencies
만약 Job이 많은 의존성들을 갖는다면, JobHandle.CombineDependencies 메서드를사용하여 그것들을 병합하라.
NativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);
JobHandle jb = JobHandle.CombineDependencies(handles);
Waiting for jobs in the main thread
메인스레드에서 Job 수행이 끝날때까지 기다리게 하고 싶으면, Jobhandle을 사용해라. 이는 Complete() 메서드를 호출하면 된다. 이점이 안전하게 Job이 실행중에도, 메인스레드에서 NativeContainer를 접근할수 있게 해준다.
Note
Job은 사용자가 schedule 한다고 해서 실행되지 않는다. 사용자가 메인스레드내에서 Job을 기다린다면, 그리고 Job 이 사용중인 nativeContainer Data를 접근하려 한다면, JobHandle.Complete()를 호출하면 된다. This method flushed the jobs from memory cache and starts the process of execution. Complete메서드를 호출하는 것은 Job의 소유권을 메인스레드에 넘겨주는 것이다. 그리고 메인스레드에서 다시 NativeContainer type 에 안전하게 접근할수 있다.
만약 데이타에 접근할 필요가 없다. 명시적으로 배치를 flush할수 있다. 이는 JobHandle.ScheduleBatchedJobs 를 호출 하면 된다. 이는 성능에 않좋은 함수이다.
//Job adding two floating point values together
public struct MyJob:IJob {
public float a;
public float b;
public NativeArray<float> result;
public void Execute(){
result[0] = a + b;
}
}
//Job adding one to a value
public struct AddOneJob:IJob {
public NativeArray<float> result;
public void Execute(){
result[0] = result[0] + 1;
}
}
NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
MyJob job1 = new MyJob();
job1.a = 10;
job1.b = 10;
job1.result = result;
JobHandle job1Handle = job1.Schedule();
AddOneJob job2 = new AddOneJob();
job2.result = result;
JobHandle job2Handle = job2.Schedule(job1Handle);
// Wait for job2 to complete
job2Handle.Complete();
// All copies of the NativeArray point to the same memory, you can access the result in your copy of the NativeArray
float aPlusB = result[0];
// Free the memory allocated by the result array
result.Dispose();