《UE4游戏开发》之 《了解UE4提供的并行函数接口》

  1. 源码位置:Engine\Source\Runtime\Core\Public\Async\ParallelFor.h
  2. 源码声明:
/** 
	*	General purpose parallel for that uses the taskgraph
	*	@param Num; number of calls of Body; Body(0), Body(1)....Body(Num - 1)
	*	@param Body; Function to call from multiple threads
	*	@param bForceSingleThread; Mostly used for testing, if true, run single threaded instead.
	*	Notes: Please add stats around to calls to parallel for and within your lambda as appropriate. Do not clog the task graph with long running tasks or tasks that block.
**/
inline void ParallelFor(int32 Num, TFunctionRef<void(int32)> Body, bool bForceSingleThread, bool bPumpRenderingThread=false)
{
	ParallelForImpl::ParallelForInternal(Num, Body,
		(bForceSingleThread ? EParallelForFlags::ForceSingleThread : EParallelForFlags::None) | 
		(bPumpRenderingThread ? EParallelForFlags::PumpRenderingThread : EParallelForFlags::None));
}
  1. 使用示例:
void ClearAllPublicExportsLoaded(const TArray<FPackageId>& PackageIds)
	{
		int32 PackageCount = PackageIds.Num();
		bool bForceSingleThreaded = PackageCount < 1024;
		/**
			*当PackageCount的数量低于1024时,使用单线程
			*对于ParallelFor,在这里调用函数体的次数为PackageCount次
			*每次调用函数体是单线程运行还是多线程,由bForceSingleThreaded决定
		*/
		ParallelFor(PackageCount, [this, &PackageIds](int32 Index)
		{
			if (FLoadedPackageRef* PackageRef = LoadedPackageStore.FindPackageRef(PackageIds[Index]))
			{
				PackageRef->ClearAllPublicExportsLoaded();
			}
		}, bForceSingleThreaded);
	}
  1. 使用时,注意尽量处理简单逻辑,避免阻塞task graph
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值