Unreal Engine has different approaches to parallelize your code and one of them is ParallelFor. It is used when it is necessary to execute code multiple times with different number-based inputs, basically what a regular for loop does. But unlike a regular for loop that executes sequentially, ParallelFor runs on different threads, with possible different order of execution.

Let’s say our task is to simply sum the values from the TArray<int32> array. It’s easiest to do this:

const TArray<int32> RequiredArray = {12, 43, 76, 23, 54};
int32 Sum = 0;
	
for (int32 Index = 0; Index < RequiredArray.Num(); ++Index)
{
	Sum += RequiredArray[Index];
}

If we have a small number of elements and the operation is not time-consuming (such as summing the elements of an array), then this is the best way to do it. But if we have a large number of elements and the operation is performance-critical, then we can use ParallelFor to speed up the process.

const TArray<int32> RequiredArray = {12, 43, 76, 23, 54, ...};
std::atomic<int32> Sum; // UE has a TAtomic template, but it is planned for complete deprecation

ParallelFor(RequiredArray.Num(), [&RequiredArray, &Sum](int32 Index)
{
	// Just for illustration purposes, as summing is too fast to be parallelized
	Sum += RequiredArray[Index];
});

or this:

const TArray<int32> RequiredArray = {12, 43, 76, 23, 54, ...};
int32 Sum;

FCriticalSection Mutex;
		
ParallelFor(RequiredArray.Num(), [&RequiredArray, &Sum, &Mutex](int32 Index)
{
	FScopeLock Lock(&Mutex);
	Sum += RequiredArray[Index];
});

These two options achieve the same goal using different approaches. The first option achieves this through atomic access to the object for modification, the second through mutexes. This is necessary to avoid race conditions , in particular data races .

On the one hand, atomic access is usually faster, but it can only be used with trivial types. On the other hand, mutexes are more versatile and can be used for any types, but can have slowdown overhead.

There is an article covering more about how to use mutexes in Unreal Engine.