잡의 Schedule 메서드를 호출하면 JobHandle을 반환합니다. 코드의 JobHandle
을 다른 잡에 대한 종속성으로 사용할 수 있습니다. 잡이 다른 잡의 결과에 종속되면 첫 번째 잡의 JobHandle
을 파라미터로 두 번째 잡의 Schedule
메서드에 다음과 같이 전달할 수 있습니다.
JobHandle firstJobHandle = firstJob.Schedule();
secondJob.Schedule(firstJobHandle);
참고: 잡의 모든 종속성은 잡 자체와 동일한 제어 스레드에 예정되어 있어야 합니다.
잡에 종속성이 많은 경우에는 JobHandle.CombineDependencies 메서드를 사용하여 결합할 수 있습니다. CombineDependencies
를 이용하면 종속성을 Schedule
메서드로 전달할 수 있습니다.
NativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);
// Populate `handles` with `JobHandles` from multiple scheduled jobs...
JobHandle jh = JobHandle.CombineDependencies(handles);
JobHandle
을 사용하면 코드가 제어 스레드에서 잡의 실행이 끝날 때까지 기다리도록 만들 수 있습니다. 이렇게 하려면 JobHandle
에 대해 Complete 메서드를 호출하십시오. 그러면 제어 스레드가 잡이 사용하던 NativeContainer에 안전하게 액세스할 수 있습니다.
참고: 잡을 예약할 때는 잡이 실행되지 않습니다. 제어 스레드에서 잡을 대기하고 있고 잡이 사용하는 NativeContainer 데이터에 액세스해야 하는 경우 JobHandle.Complete
메서드를 호출할 수 있습니다. 이 메서드는 메모리 캐시의 잡을 플러시하고 실행 프로세스를 시작합니다. JobHandle
에 대해 Complete
를 호출하면 해당 잡의 NativeContainer
타입에 대한 소유권을 제어 스레드에 반환합니다. 제어 스레드에서 해당 NativeContainer
타입에 다시 안전하게 액세스하려면 JobHandle
에 대해 Complete
를 호출해야 합니다. 잡 종속성에서 온 JobHandle
에 대해 Complete
를 호출하여 소유권을 제어 스레드로 반환할 수도 있습니다. 예를 들어, jobA
에 대해 Complete
를 호출하거나, jobA
에 종속된 jobB
에 대해 Complete
를 호출할 수 있습니다. 두 경우 모두 Complete
호출 이후 제어 스레드에서 jobA
가 사용하는 NativeContainer
타입에 안전하게 액세스할 수 있습니다.
데이터에 액세스할 필요가 없는 경우에는 배치를 명시적으로 플러시해야 합니다. 이렇게 하려면 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;
}
}
메인 스레드 코드:
// Create a native array of a single float to store the result in. This example waits for the job to complete
NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
// Setup the data for job #1
MyJob jobData = new MyJob();
jobData.a = 10;
jobData.b = 10;
jobData.result = result;
// Schedule job #1
JobHandle firstHandle = jobData.Schedule();
// Setup the data for job #2
AddOneJob incJobData = new AddOneJob();
incJobData.result = result;
// Schedule job #2
JobHandle secondHandle = incJobData.Schedule(firstHandle);
// Wait for job #2 to complete
secondHandle.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();
2018–06–15 페이지 게시됨
2018.1에서 공개된 C# 잡 시스템 NewIn20181
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.