Let’s say I’ve got an array with similar but independent data, and would need to run the same non-trivial but completely independent function on each entry. I’ve seen that there exists a function named ParallelFor
that would be perfect for this purpose, as it splits the work in several task graph tasks and blocks until all of them are complete.
Now the question is, if I need to do something special afterwards to be certain that the processed data is available in the game thread, like setting up memory barriers?
What I mean: Does this code work, or do I need to put something in place of the comment to ensure that the data set in the ParallelFor
is available for DoSomethingNotParallel()?
Is there a better way that ensures that I get the results back before the ParallelFor
finishes?
void processData(TArray<DataType>& Data)
{
ParallelFor(Data.Num(), [&](int32 CurrIdx)
{
Data[CurrIdx] = DoSomethingParallel(Data[CurrIdx]);
}
//Do I need to put a memory barrier or something else here?
DoSomethingNotParallel(Data);
}