Multithreading on server (theoretical question)

Greetings dear community!

I know that ue4 provides some multithreading functionality and I’ve read Rama’s tutorials about subject (THANK YOU, RAMA!). So my question is more theoretical, because I didn’t have an experience with multitasking before.

Lets assume that I have 100 people on one server. During game run I write (somehow) some data (somewhere). At some point (say once in a minute) I need to make some complicated-very-heavy stuff with this data. For that I’ll have one special thread. But how can I organize my data a way it will not lock the main thread?

More precise: In main thread I’m sending some data to special thread. Let’s assume that this data is just a number (for example, 10e6 or 30e7) of how much should I calculate FMath::sin(). This number should be written in special container (what container? I assume there should be some thread-safe container?). Once a minute special thread should take this numbers from special container and start calculations of FMath::sin(). But how can I safely manage this work, i.e. don’t write to special container while calculate numbers? Is there a better way?

One more question: Does ue4 containers thread-safe?

Take a look at the FTaskGraphInterface. It’s used to queue work that is to be done parallel to the game thread. There are also primitives for synchronizing access to variables that you can use to make these parallel tasks threadsafe.

Thank you.

What kind of primitives? Currently I’m using FTaskGraphInterface::WaitUntilTaskCompletes in my main thread. Is it correct?

Also a question - there’s a function FSimpleDelegateGraphTask::CreateAndDispatchWhenReady(). But how does it dispatches it’s completion? I could find only FGraphEventRef::IsComplete() that should be checked with some interval.

The engines comes also with event signaling using FEvent and memory barriers FPlatformMisc::MemoryBarrier().

An example on using events can be found within the UDP message beacon, just search for EndpointLeftEvent.