skip to Main Content

Implementing Multithreading in UE4

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:

mt_inputs

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.

This Post Has 12 Comments

  1. I’m waiting for the Blueprints version…will this ever be possible in Blueprints only (no C++ at all)?

    1. Well I’m not seeing something on the UE4 roadmap so my guess is that we won’t see this in the near future. Considering the work that has been done in Blueprints so far I believe that a Blueprintable version of this post is do-able (technically speaking). Maybe an employee of Epic can provide you with a solid answer on that!

      -Orfeas

      1. Thanks for your reply. I really do believe Blueprints is awesome, but still very limited. Adding multithreading would simply take it to the next level, and as you’re saying, there’s no reason why it wouldn’t be possible from a technical perspective.

  2. Does this need to be in a character class? I added it to an actor class that I want to use it for calculating noise value in the background. I dropped the UFunctions in the protected section in my header. The namepspace and PrimeCalculationAsyncTask stuff I dropped below outside the actor class. Intellisense doesn’t give me any trouble, but when I compile I see LNK2001 errors. External sumbol “Protected”

  3. In ue4 ,whether or not need us to consider c++11 feature like “atomic operation” ,“lock” and “competition” ?

    1. I haven’t explored if the engine uses that under the hood.
      So, if I can implement the features I want with the systems provided, I wouldn’t bother (unless something goes awry).

      -Orfeas

  4. Hello! I started finding information about multithreading and found myself here. Your tutorial is very clear and very well commented. Thank you for your effort. My question is, how is this so different than Ramas version? How would one know, which one to implement to project? This is the other version: https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4

    It is much more longer and very different from this one. They are doing the same thing however: Finding prime number. I just got confused of what is the difference of two, could you clarify for it? Thank you very much!

Leave a Reply to starkium Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top