Version: 2022.1
调度作业
ParallelFor 作业

JobHandle 和依赖项

调用作业的 Schedule 方法时,将返回 JobHandle。可以在代码中使用 JobHandle 作为其他作业的依赖项。如果一个作业依赖于另一个作业的结果,则可以将第一个作业的 JobHandle 作为参数传递给第二个作业的 Schedule 方法,如下所示:

JobHandle firstJobHandle = firstJob.Schedule();
secondJob.Schedule(firstJobHandle);

Note: All of a job’s dependencies must be scheduled on the same control thread as the job itself.

合并依赖项

如果一个作业具有许多依赖项,则可以使用 JobHandle.CombineDependencies 方法合并这些依赖项。CombineDependencies 可以将依赖项传递给 Schedule 方法。

NativeArray<JobHandle> handles = new NativeArray<JobHandle>(numJobs, Allocator.TempJob);

// 使用来自多个调度作业的 `JobHandles` 填充 `handles`...

JobHandle jh = JobHandle.CombineDependencies(handles);

Waiting for jobs in the control thread

Use JobHandle to force your code to wait in the control thread for your job to finish executing. To do this, call the method Complete on the JobHandle. At this point, you know the control thread can safely access the NativeContainer that the job was using.

Note: Jobs do not start executing when you schedule them. If you are waiting for the job in the control thread, and you need access to the NativeContainer data that the job is using, you can call the method JobHandle.Complete. This method flushes the jobs from the memory cache and starts the process of execution. Calling Complete on a JobHandle returns ownership of that job’s NativeContainer types to the control thread. You need to call Complete on a JobHandle to safely access those NativeContainer types from the control thread again. It is also possible to return ownership to the control thread by calling Complete on a JobHandle that is from a job dependency. For example, you can call Complete on jobA, or you can call Complete on jobB which depends on jobA. Both result in the NativeContainer types that are used by jobA being safe to access on the control thread after the call to Complete.

否则,如果您不需要访问数据,则需要显式刷新批次。为此,请调用静态方法 JobHandle.ScheduleBatchedJobs。请注意,调用此方法会对性能产生负面影响。

多个作业和依赖项的示例

作业代码

// 将两个浮点值相加的作业
public struct MyJob : IJob
{
    public float a;
    public float b;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = a + b;
    }
}

// 将一个值加一的作业
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);

// 设置作业 #1 的数据
MyJob jobData = new MyJob();
jobData.a = 10;
jobData.b = 10;
jobData.result = result;

// 调度作业 #1
JobHandle firstHandle = jobData.Schedule();

// 设置作业 #2 的数据
AddOneJob incJobData = new AddOneJob();
incJobData.result = result;

// 调度作业 #2
JobHandle secondHandle = incJobData.Schedule(firstHandle);

// 等待作业 #2 完成
secondHandle.Complete();

// NativeArray 的所有副本都指向同一内存,您可以在"您的"NativeArray 副本中访问结果
float aPlusB = result[0];

// 释放由结果数组分配的内存
result.Dispose();

  • 2018–06–15 页面已发布

  • 2018.1 版中公开了 C# 作业系统 NewIn20181

调度作业
ParallelFor 作业