Version: 2020.1
언어: 한국어
잡 예약
ParallelFor 잡

JobHandle 및 종속성

잡의 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

잡 예약
ParallelFor 잡
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961