Daily Unreal Column #19 - ParallelFor
Whenever you are processing big set of data, ParallelFor function might be your best friend.
ParallelFor
is an amazing function that allows you to process data by defining how many elements it should process and a lambda function that will get executed for each. The underlying implementation will split the workload into multiple threads making the multi-threading basically invisible to the user. However, please be very mindful of the operations you are performing inside the lambda function as all of them is going to be executed OUTSIDE of the game thread, so modifying actors is not the greatest idea.
TArray<FSomeData> SomeData;
ParallelFor(SomeData.Num(), [&SomeData](int32 Index)
{
FSomeData& Foo = SomeData[Index];
// Process it here
})
// All elements have been processed here, the Game Thread
// will wait until all other threads issued by ParallelFor
// finish processing their stuff.