# Multithreading function that runs on all logic threads except Game Thread

**URL:** <https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443>\
**Category:** C++\
**Tags:** multithreading, question, CPP\
**Created:** [April 22, 2024, 12:28pm UTC](https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443 "2024-04-22T12:28:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sitiana](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sitiana/32/1194605_2.png) [@Sitiana](https://forums.unrealengine.com/u/Sitiana)\
**Post date:** [April 22, 2024, 12:28pm UTC](https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443/1 "2024-04-22T12:28:26Z")

</div>

Hello Devs,

I’m working on some things that require millions of traces and for that I need to use multithreading.  
The problem is that I need to have a function that is not blocking Game Thread, but is using all other logical threads like ParrarelFor. Do you know how I could achieve that?

Because I can use FRunnable which will be async, but it’s slow as it’s on just 1 logic thread. ParrarelFor is much quicker, but it’s blocking also GameThread. Is there something in between? Or maybe I can somehow prevent ParrarelFor from running on GameThread?

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![StarScream\_83](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/starscream_83/32/487170_2.png) [@StarScream\_83](https://forums.unrealengine.com/u/StarScream_83)\
**Post date:** [June 19, 2024, 1:34am UTC](https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443/2 "2024-06-19T01:34:32Z")

</div>

FRunnable can’t receive UObjects, it can only be used to do mathematical calculations, what you want is to use AsyncTask, this will run your function asyncronously on the game thread, it will not block the game thread, to a certain limit, i used it to spawn 9000 instanced meshes seamlessly with no lag spike.  
use FRunnable for calculations, and AsyncTask to manipulate a large amount of actors (or line traces) in the game world.  
here is where you can learn it:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/f/6/ef60e5daec62991054a7ace2e5e8c104781a3215.jpeg "UE4/UE5 How to using multi thread (with C++) (using NonAbandonableTask, AsyncTask)") ](https://www.youtube.com/watch?v=TpDj__0ewXA)

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/f/b6fcebc2811fdcf9fc0d946d5405cc49da668c57.jpeg "The Simplest Multi-threading in Unreal Engine") ](https://www.youtube.com/watch?v=_Kchr20XcOQ)

best of luck.

---

<div class="post-metadata">

**Author:** ![Sitiana](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sitiana/32/1194605_2.png) [@Sitiana](https://forums.unrealengine.com/u/Sitiana)\
**Post date:** [June 19, 2024, 7:11pm UTC](https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443/3 "2024-06-19T19:11:00Z")

</div>

Thanks for your answer. We indeed used an AsyncTask, but it wasn’t enough. Because normally AsyncTask uses 1 worker thread and our calculations were too heavy to use just one thread.

What we used was ParallelForWithExistingTaskContext inside an AsyncTask and thanks to that we could limit used worker threads for example to 1/2 of all available logic threads. So it’s not blocking game thread and it’s much faster.

Thanks for your great tips!

---

<div class="post-metadata">

**Author:** ![StarScream\_83](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/starscream_83/32/487170_2.png) [@StarScream\_83](https://forums.unrealengine.com/u/StarScream_83)\
**Post date:** [June 20, 2024, 1:35am UTC](https://forums.unrealengine.com/t/multithreading-function-that-runs-on-all-logic-threads-except-game-thread/1815443/4 "2024-06-20T01:35:52Z")

</div>

glad to hear you found a solution, i didn’t know about parallel for, i’ll look it up, one more tool to add to our arsenal.
