In this post we’re going to see how easy it is to achieve multithreading inside UE4 using C++. Taken directly from the corresponding wikipedia page, multithreading is the ability of a CPU, to execute multiple processes or threads concurrently.

Modern software applications are designed in a way to inform the user at any time about their state. For example, if an application is stuck somewhere, it will most likely display a corresponding indication (ie a progress bar or a loading ring). Usually, this is done by dividing the logic of the application into two (or more) threads. The main thread is always responsible for the UI of the application, since it displays or hides all the progress indications, while the other threads perform their logic in the “background”.

Since the main thread in software applications is responsible for the UI, you can imagine that the main thread inside Unreal Engine 4 is the one responsible for the rendering. From this point on, we will refer to it as the game thread.

If you try to perform heavy operations inside the game thread, you will most likely experience a game freeze (depending on your PC and the calculations you’re performing). In this post, I’ve created a simple function which finds the first N prime numbers (N is specified through the Editor). Moreover, I’ve created two inputs – one of them calls that function in the game thread, while the other one calls the same function in a different thread.

Before we get started, here is the end result:

Setting up our project

For this post I’ve created a Third Person C++ Project template. Inside the character’s header file I’ve added the following code:

Then, inside the header file of the character’s class declaration and right after it’s implementation, I’ve added the following namespace, which contains the function that performs the actual calculations:

Later on, we will add one more class inside the header file of the character but not inside the character’s class. We declared a namespace which contains the static function CalculatePrimeNumbers in order to be able to access the same function from different code classes.

Having said that, here is the implementation of the CalculatePrimeNumbers function:

Add an empty implementation of the CalculatePrimeNumbersAsync function and compile and then your code. Then, specify two key binds using your character’s Blueprint like the following image suggests:


Creating a Task

When it comes to multithreading, you will hear a lot about Tasks. To simplify things, a Task is a piece of code which runs on any thread. We are going to create a new class which will execute the CalculatePrimeNumbers function, in another thread.

To add this class, we don’t have to use the normal Add a C++ Class workflow through the UE4 Editor. We will add our class by hand!

So, which class are we going to inherit this time? Well, we need to locate a class that provides some built-in functionality, in order to create and use a Task. I’ve already searched the source code of the engine and located the necessary class for our case so you don’t have to worry!

Right after the declaration of your namespace, add the following class:

When you’re done with the above code, add the following implementation inside the CalculatePrimeNumberAsync:

Compile and test your code! Don’t forget to edit the MaxPrime variable through your Editor to have similar results to the video demonstrated above!

In case you got confused, here is the whole header file of my character class:

PS: In case you want to know more about Multithreading inside UE4, I suggest to locate the file AsyncWork.h inside the source code of the engine.