# Multi-threading

**URL:** <https://forums.unrealengine.com/t/multi-threading/2954>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 20, 2014, 9:32pm UTC](https://forums.unrealengine.com/t/multi-threading/2954 "2014-04-20T21:32:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AutumnPyrri](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AutumnPyrri](https://forums.unrealengine.com/u/AutumnPyrri)\
**Post date:** [April 20, 2014, 9:32pm UTC](https://forums.unrealengine.com/t/multi-threading/2954/1 "2014-04-20T21:32:17Z")

</div>

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

EDIT:  
For anyone else wondering about this, a staff member on the UE4 Answerhub gave me this solution:

> [@](#):
>
> 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 🙂

---

<div class="post-metadata">

**Author:** ![Dig\_Squid](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dig_squid/32/1145423_2.png) [@Dig\_Squid](https://forums.unrealengine.com/u/Dig_Squid)\
**Post date:** [April 21, 2014, 8:13am UTC](https://forums.unrealengine.com/t/multi-threading/2954/2 "2014-04-21T08:13:02Z")

</div>

You probably want to use the tick task system, but I’m not sure if there are many docs on that yet though. You can look how the standard ActorComponent tick function is set up using FTickFunction. The key thing is that you will set bRunOnAnyThread to true, but you CANNOT do things like access/create/destroy UObjects safely from other threads.

---

<div class="post-metadata">

**Author:** ![vblanco](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vblanco](https://forums.unrealengine.com/u/vblanco)\
**Post date:** [April 21, 2014, 10:48am UTC](https://forums.unrealengine.com/t/multi-threading/2954/3 "2014-04-21T10:48:36Z")

</div>

Im helping him as what he is trying to do is something very similar to what im making here [Log in with Atlassian account](https://bitbucket.org/victorblancoruiz9/cubicengine).  
My voxel engine launches a “generator” thread when it finds that a regeneration of the voxel world is needed, such as at beginning of the game or when i start generating the voxels when i move, that starts calculating the vertex arrays and storing them into the chunk objects, when its done generating one chunk, it marks it as “finished”, and on the next Tick of the chunk it submits the vertex array to the rendering engine. Im using a std::thread there in a very very basic way

std::thread testthread(&VoxelWorldGenerator::UpdateField,this,fieldCenter); // launch generator thread, it then goes calling the generate vertex array functions in each block of the whole environment  
testthread.detach(); // and then it leaves it alone, as we dont really need to modify that tread after that.

I wonder if something similar exists in UE4 libraries, or if we can just use std::vector from C++11 there.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [April 21, 2014, 9:53pm UTC](https://forums.unrealengine.com/t/multi-threading/2954/4 "2014-04-21T21:53:38Z")

</div>

> [@vblanco;20906](#):
>
> when its done generating one chunk, it marks it as “finished”, and on the next Tick of the chunk it submits the vertex array to the rendering engine. Im using a std::thread there in a very very basic way
> 
> std::thread testthread(&VoxelWorldGenerator::UpdateField,this,fieldCenter); // launch generator thread, it then goes calling the generate vertex array functions in each block of the whole environment  
> testthread.detach(); // and then it leaves it alone, as we dont really need to modify that tread after that.

Dear Victor,

Could you write a wiki tutorial on multi-threading?

Rama

---

<div class="post-metadata">

**Author:** ![vblanco](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vblanco](https://forums.unrealengine.com/u/vblanco)\
**Post date:** [April 21, 2014, 9:54pm UTC](https://forums.unrealengine.com/t/multi-threading/2954/5 "2014-04-21T21:54:54Z")

</div>

> [@Rama;21145](#):
>
> Dear Victor,
> 
> Could you write a wiki tutorial on multi-threading?
> 
> Rama

when i figure out how the hell it works, be sure ill make a tutorial of it.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [April 22, 2014, 4:10am UTC](https://forums.unrealengine.com/t/multi-threading/2954/6 "2014-04-22T04:10:46Z")

</div>

**[FONT=Comic Sans MS]Multi-Threading Wiki Tutorial**

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

**[My new Wiki tutorial on Multi-threading!](https://wiki.unrealengine.com/Multi-Threading:_Task_Graph_System)**

I am happy to announce I figured out how to use the UE4 Task Graph System to do multi-threading!

Here’s a video of my game calculating the first 50,000 prime numbers while I continue to play around in the main game thread!

!

🙂

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/d/ccd1bd8d1d6da9686b9c9b2bb2b49f3e6d9fe16e.jpeg "MultiThreadingPrimeNumbers") ](https://www.youtube.com/watch?v=cgELOodtoSU)

```auto

**Comparing Results**

You can compare my in-game computed results displayed in the player console to this web page of the first 50,000 prime numbers!

http://www.cs.arizona.edu/icon/oddsends/primes.htm

Rama
```

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [April 22, 2014, 8:57am UTC](https://forums.unrealengine.com/t/multi-threading/2954/7 "2014-04-22T08:57:03Z")

</div>

sweet Rama that’s cool haha, could you make a plugin that lets me download your grasp of C++ directly to my mind ;)?

---

<div class="post-metadata">

**Author:** ![AutumnPyrri](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AutumnPyrri](https://forums.unrealengine.com/u/AutumnPyrri)\
**Post date:** [April 22, 2014, 6:36pm UTC](https://forums.unrealengine.com/t/multi-threading/2954/8 "2014-04-22T18:36:23Z")

</div>

I was told by an Epic employee that the Task Graph system is only good for small workloads. Rendering chunks of voxels is probably not in the scope of what Task Graph can do.

Still its extremely useful, awesome tutorial!

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [April 22, 2014, 6:50pm UTC](https://forums.unrealengine.com/t/multi-threading/2954/9 "2014-04-22T18:50:05Z")

</div>

> [@Ad-man;21552](#):
>
> I was told by an Epic employee that the Task Graph system is only good for small workloads. Rendering chunks of voxels is probably not in the scope of what Task Graph can do.
> 
> Still its extremely useful, awesome tutorial!

well I was actually thinking about your example

You can do all the calculations in the task threads, and then send the final output data to the game thread, and the game thread can have a short timer, like 0.01 seconds, to actually check what data the other threads are returning and spawn / create the required objects using the pre-computed data.

As each chunk of data is processed in game thread it is removed by that 0.01 second timer so that it does not recreate the same meshes twice.

If you are doing lots of complicated calculations in advance and can just send final spawn / creation data back to the game thread that would work great!

Good luck!

Rama

---

<div class="post-metadata">

**Author:** ![tgraupmann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tgraupmann](https://forums.unrealengine.com/u/tgraupmann)\
**Post date:** [December 12, 2014, 3:15am UTC](https://forums.unrealengine.com/t/multi-threading/2954/10 "2014-12-12T03:15:32Z")

</div>

Threading question. I’m getting a callback from Android which is not in the main game thread. The callback has access to my AActor. What’s the recommended way to get the main game thread so I can interact with AActors?

\*\*\*\* NOT in the main thread \*\*\*\* I have parameters from a callback that needs to go to the main thread…

\*\*\* Main Game Thread Over Here \*\*\*\* plz execute my code

It’s a little different than the wiki example, since I’m not spawning threads to do work. The work is already done and ready for the main thread.

The error:

D/UE4 ( 7738): Assertion failed: IsInGameThread() [File:C:\unreal\UnrealEngine\Engine\Source\Runtime\Engine\Private\TimerManager.cpp] [Line: 223]

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4fe9f58b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4fe9f58b1](https://forums.unrealengine.com/u/anonymous_user_4fe9f58b1)\
**Post date:** [December 12, 2014, 4:21am UTC](https://forums.unrealengine.com/t/multi-threading/2954/11 "2014-12-12T04:21:46Z")

</div>

You’ll need to invert that logic and have the main thread pull the data provided by the callback when it is actually in a state to handle Actor data (i.e.: during a tick). The easiest way to do this is with a centralized, unmanaged structure where you can queue up data for consumption. Put a FThreadSafeCounter on there, have the callback increment it when it is issued, then during actor tick, have it check if the counter is \> 0 and consume the queued data if so.

Regarding your error, are you issuing timer delegates on a different thread? Because that won’t work, as you’ve found out. Whatever structure is issuing those timers could instead poll game time and manually call the delegates when the wait time elapses. I **think** UWord::TimeSeconds is thread safe, that’d be a good place to start.

-

---

<div class="post-metadata">

**Author:** ![tgraupmann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tgraupmann](https://forums.unrealengine.com/u/tgraupmann)\
**Post date:** [December 12, 2014, 5:36am UTC](https://forums.unrealengine.com/t/multi-threading/2954/12 "2014-12-12T05:36:50Z")

</div>

That’s pretty much exactly what I implemented.

I used a Tick on my SDK actor to poll any available work that I shoved into lambda] functions from my threaded callbacks.

For whatever reason `PrimaryActorTick.bCanEverTick = true;` was needed to get the tick events to fire.  
[https://answers.unrealengine.com/questions/1855/actor-extending-tick-c.html](https://answers.unrealengine.com/questions/1855/actor-extending-tick-c.html)

Thanks for the hints!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4fe9f58b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4fe9f58b1](https://forums.unrealengine.com/u/anonymous_user_4fe9f58b1)\
**Post date:** [December 12, 2014, 5:46am UTC](https://forums.unrealengine.com/t/multi-threading/2954/13 "2014-12-12T05:46:40Z")

</div>

Yeah, there is a fair amount of overhead from managing ticking, so the default is not to do it. That’s a tricky surprise for newcomers trying to tick their actor that doesn’t derive from an already ticking class like Pawn.

---

<div class="post-metadata">

**Author:** ![tgraupmann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tgraupmann](https://forums.unrealengine.com/u/tgraupmann)\
**Post date:** [December 13, 2014, 12:18am UTC](https://forums.unrealengine.com/t/multi-threading/2954/14 "2014-12-13T00:18:57Z")

</div>

Here’s the syntax I ended up going with.  
[https://github.com/tgraupmann/UnrealEngine/blob/4.6-OUYA/Engine/Source/Runtime/Engine/Private/OuyaSDK/OuyaSDK.cpp#L423](https://github.com/tgraupmann/UnrealEngine/blob/4.6-OUYA/Engine/Source/Runtime/Engine/Private/OuyaSDK/OuyaSDK.cpp#L423)

void CustomCallbacksInitOuyaPlugin::OnFailure(int errorCode, const std::string& errorMessage)  
{  
\_\_android\_log\_print(ANDROID\_LOG\_VERBOSE, LOG\_TAG, “OnFailure: errorCode=%d errorMessage=%s”, errorCode, errorMessage.c\_str());

```
_resultErrorCode = errorCode;
_resultErrorMessage = errorMessage;

if (_actor)
{
	_actor-&gt;_mainThreadActions.emplace_back(]()
	{
		_actor-&gt;OnFailureInitOuyaPlugin.ExecuteIfBound(_resultErrorCode, _resultErrorMessage.c_str());
	});
}

```

}

I used some static fields on some callback classes to keep things neat.

And then a vector of functions to execute in my update tick.  
[https://github.com/tgraupmann/UnrealEngine/blob/4.6-OUYA/Engine/Source/Runtime/Engine/Classes/OuyaSDK/OuyaSDK.h#L242](https://github.com/tgraupmann/UnrealEngine/blob/4.6-OUYA/Engine/Source/Runtime/Engine/Classes/OuyaSDK/OuyaSDK.h#L242)

// Use the tick event to fire callbacks on the main game thread  
virtual void Tick(float deltaSeconds) OVERRIDE;

// hold functions to execute in the main thread  
std::vector\<std::function\<void()\>\> \_mainThreadActions;

Works like a charm.
