Unreal – Multi-threaded Parallel For Loop

ParallelFor is UE's built-in For loop that supports multi-threaded parallel processing tasks and is quite commonly used in rendering systems.

  • Calling method function signature
void ParallelFor(int32 Num, TFunctionRef<void(int32)> Body, bool bForceSingleThread, bool bPumpRenderingThread = false);
void ParallelFor(int32 Num, TFunctionRef<void(int32)> Body, EParallelForFlags Flags = EParallelForFlags::None);
void ParallelForTemplate(int32 Num, const FunctionType& Body, EParallelForFlags Flags = EParallelForFlags::None);
void ParallelForWithPreWork(int32 Num, TFunctionRef<void(int32)> Body, TFunctionRef<void()> CurrentThreadWorkToDoBeforeHelping, bool bForceSingleThread, bool bPumpRenderingThread = false);
void ParallelForWithPreWork(int32 Num, TFunctionRef<void(int32)> Body, TFunctionRef<void()> CurrentThreadWorkToDoBeforeHelping, EParallelForFlags Flags = EParallelForFlags::None);
  • parallel mode
enum class EParallelForFlags
{
    // 默认
    None,

    // 单线程模式 主要用于调试
    ForceSingleThread = 1,

    // 在线程中提供更好的任务分配 将花费更多的同步成本
    // 这应该用于耗时多变的计算任务
    Unbalanced = 2,

    // 渲染线程 如果是在渲染线程调用 需要保证 ProcessThread 处于空闲状态
    PumpRenderingThread = 4,

    // 任务应该在后台优先级的线程上运行
    BackgroundPriority = 8,
};
  • How to use
ParallelFor(/* 循环次数 */,
    [&](int32 Index)
    {
        // Code
    },
    /* 并行方式 */
);

Post Reply