Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

Does Parallel For need manual barriers?

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?

Product Version: UE 4.18
more ▼

asked Jul 27 '18 at 07:28 PM in C++ Programming

avatar image

232 4 11 14

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

I couldn't stop myself from reading engine source code. The answer is actually pretty easy to find, if one knows where to look...

ParallelFor uses FEvent to tell the game thread that the individual tasks that are created have finished. FEvent is basically an abstraction of thread synchronization using events, and behaves like Windows Event objects (on Windows it's just a wrapper around those). This means, that whenever the game thread gets informed of a parallel-for task being finished, there's a memory barrier in place, so that the data in the main thread is guaranteed to contain all changes done within the parallel for.

In other words: Parallel For is perfect for what I want to do, and no additional synchronization is needed.

more ▼

answered Jul 27 '18 at 10:14 PM

avatar image

232 4 11 14

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question