How do I set up Multi-threading?

Hello,

I was looking for a way to multi-thread so my world generation could continue during runtime, but I cant find a single thing on multi-threading in UE4 anywhere. Can someone point me where to look or give me some tips on how to do it?

Thanks!
~Adam

You probably want to check out FRunnable and FRunnableThread. FRunnable encapsulates a worker that does the actual work. You inherit from this class, then implement the Run() method (and possibly others, such as Init(), Stop() and Exit()). You can then create a thread that uses this worker with FRunnableThread::Create(). You should be able to find many examples in the code base.

We have some other multi-threading mechanisms in the Engine, but your use case may not be a good fit. If your units of work are very small, you can take a look at the Task Graph system, which provides a nice way of performing task based multi-threading, and which is particularly useful if your tasks have dependencies to each other. You can find some usage examples by searching for CreateTask() in the code base.

Please note that the UObject sub-system is currently not thread-safe, so depending on what you’re trying to do for world generation, you have to be extremely careful and know what you’re doing :slight_smile:

#Thank you GmPreussner

Inspired by your answer I have tested and composed a Wiki Tutorial on using the Task Graph system!

#My Wiki Tutorial on Multi-Threading

I am including my entire source code! (Legal: I wrote all of it)

#Video

I am computing the first 50,000 prime numbers in the background while I continue to play around in the main game thread!

:slight_smile:

Rama