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, this will be enough. But if we have a very large number of elements, then we can use ParallelFor, which will be executed in different threads and potentially speed up this. According to the above code, it will look like this:

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)
{
	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)
{
	Mutex.Lock();
	Sum += RequiredArray[Index];
	Mutex.Unlock();
});

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 so that no exceptions occur when variables are changed simultaneously by different threads.

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.

Analyze what suits you best for your specific case and use as needed.