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"

How to use FPThreadsCriticalSection make data threadsafe

I have been searching the web for examples of how to use FPTreadsCriticalSection, but cannot find anything. I have one thread that writes to a TArray and another one reading from the same TArray How can I use FPThreadsCriticalSection to make the TArray threadsafe? Or any other ways of syncronizing? I am not very experienced with threading in c++

Product Version: UE 4.7
more ▼

asked Jul 28 '15 at 05:51 PM in C++ Programming

avatar image

15 3 7 10

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first


I think You should create a FCriticalSection (or FPThreadsCriticalSection if You want) in the object where the TArray is. For example:

 FCriticalSection MyCritialSection;

Everytime the thread want an access to the TArray (read or write) it should launch the MyCritialSection.Lock() function and when it finish using the TArray launch the MyCritialSection.Unlock() function.

Make sure the Lock() won't be run twice by the same thread before using Unlock().

Also keep in mind to keep critical section as short as possible to not hunger the second thread.

more ▼

answered Jul 30 '15 at 08:52 AM

avatar image

755 24 48 145

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Bad things are going to happen (locks/crashes) if you somehow fail to unlock the CriticalSection - there could be multiple things that can happen to skip this step, more so if you are using exeption handling.

You can also use FScopeLock in combination of FCriticalSection. Constructor of that thing is going to lock the CriticalSection and the dtor is going to unlock it.

     // scope start
     FScopeLock ScopeLock(&MyCriticalSection);
     // scope end, critical section will be unlocked here
more ▼

answered Jan 12 '18 at 08:30 AM

avatar image

31 2 4 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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