虚幻 – TaskGraph 多线程任务图

class FTestTask
{
public:

    FTestTask(const FString& InTaskName) : TaskName(InTaskName) {  }

    TStatId GetStatId() { RETURN_QUICK_DECLARE_CYCLE_STAT(TestTask, STATGROUP_TaskGraphTasks); }

    ENamedThreads::Type GetDesiredThread() { return ENamedThreads::AnyThread; }

    static ESubsequentsMode::Type GetSubsequentsMode() { return ESubsequentsMode::TrackSubsequents; }

    void DoTask(ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent)
    {
        for (int32 Index = 0; Index < 5; ++Index)
        {
            UE_LOG(LogTemp, Log, TEXT("Task %s executing, index %d."), *TaskName, Index);
        }

        UE_LOG(LogTemp, Log, TEXT("Task %s completed!"), *TaskName);
    }

private:

    FString TaskName;

};

UE_LOG(LogTemp, Log, TEXT("------ Begin Tasks ------"));
FGraphEventRef A = TGraphTask<FTestTask>::CreateTask().ConstructAndDispatchWhenReady(TEXT("A"));
FGraphEventRef B = TGraphTask<FTestTask>::CreateTask().ConstructAndDispatchWhenReady(TEXT("B"));
FGraphEventArray DependencyForC = { A, B };
FGraphEventRef C = TGraphTask<FTestTask>::CreateTask(&DependencyForC).ConstructAndDispatchWhenReady(TEXT("C"));
FGraphEventArray Tasks = { A, B, C };
FTaskGraphInterface::Get().WaitUntilTasksComplete(Tasks, ENamedThreads::GameThread);
UE_LOG(LogTemp, Log, TEXT("------ End Tasks ------"));
LogTemp: ------ Begin Tasks ------
LogTemp: Task A executing, index 0.
LogTemp: Task B executing, index 0.
LogTemp: Task A executing, index 1.
LogTemp: Task B executing, index 1.
LogTemp: Task A executing, index 2.
LogTemp: Task B executing, index 2.
LogTemp: Task A executing, index 3.
LogTemp: Task B executing, index 3.
LogTemp: Task A executing, index 4.
LogTemp: Task B executing, index 4.
LogTemp: Task A completed!
LogTemp: Task B completed!
LogTemp: Task C executing, index 0.
LogTemp: Task C executing, index 1.
LogTemp: Task C executing, index 2.
LogTemp: Task C executing, index 3.
LogTemp: Task C executing, index 4.
LogTemp: Task C completed!
LogTemp: ------ End Tasks ------

发表回复