# How to multi thread of wait for new tick?

**URL:** <https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 9, 2017, 7:16pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370 "2017-01-09T19:16:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [January 9, 2017, 7:16pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/1 "2017-01-09T19:16:23Z")

</div>

So I am working on a plugin, and since it has some very heavy tasks I am looking for a way to execute certain functions on another thread then the game thread, because otherwise the performance drop is just to big. These functions don’t have to be completed very fast, so I can afford to lose a bit of speed in these functions. The problem is that I can’t find any good tutorials on multithreading, I have read some things about FRunnable and FRunnaleThread, but I don’t fully understand it.  
What also could be a solution, is allowing these functions to be partially executed when they tick, and then one or more ticks after it continue and finnish is, but I don’t know if it is possible and I would like to multhithread it.

Do you know anything about this or know a good tutorial, please help me.

-

---

<div class="post-metadata">

**Author:** ![Dartanlla](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dartanlla/32/724052_2.png) [@Dartanlla](https://forums.unrealengine.com/u/Dartanlla)\
**Post date:** [January 9, 2017, 7:19pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/2 "2017-01-09T19:19:01Z")

</div>

Here is a tutorial from Rama on how to use multithreading in UE4:

> **[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 — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [January 10, 2017, 7:46pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/3 "2017-01-10T19:46:23Z")

</div>

Dartanlla, I have found that tutorial already, but I don’t know if I can implement it in my system, this piece of pseudo code should give a better idea of what I want:

```
.h

class X : Actor
{
//variables for functions
//custom struct with a function inside
//array
void update(); //checks if I want to load or generate
void Generate(); //Sets up variables for generation; Calls GenOneSide twice
void GenOneSide(float PlusX, float PlusY); //Generates one side
//GenOneSide is very heavy, and I want that function to be on a seperate thread, or let it wait for a new tick at certain points
};

```

How do I let the GenOneSide function run on another thread? It will need some variables that are in class X, but of course I can change things around.

Thanks in advance

-

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [January 17, 2017, 6:17pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/4 "2017-01-17T18:17:26Z")

</div>

I have come a lot further (I think) but now I still crash when trying to execute a certain bit of code on the game thread. As I describe in this post: [How to execute code on game thread, using AsyncTask or something like that? - C++ - Epic Developer Community Forums](https://answers.unrealengine.com/questions/548541/crash-using-asynctask.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [January 18, 2017, 3:03pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/5 "2017-01-18T15:03:08Z")

</div>

Okay I got that to work, only one strange thing is still happening:

```
	AsyncTask(ENamedThreads::GameThread, [=]()
	{
		Debug(Vector1, Vector2, Delta); //Detla = 650.000183
	});
	k = 0;
	UE_LOG(LogTemp, Warning, TEXT("Delta = %f"), Delta); //Delta = -431602080.000000
	UE_LOG(LogTemp, Warning, TEXT("k = %f"), k); //k = 0
	for (k; k < Delta; k++)
	{
//Not executed
		UE_LOG(LogTemp, Warning, TEXT("k = %f"), k);
		UE_LOG(LogTemp, Warning, TEXT("Entered for loop k!"));
	}
	for (int i = 0; i < Delta; i += 50)
	{

```

``//Not executed  
UE\_LOG(LogTemp, Warning, TEXT(“Entered for loop i!”));  
FHitOut Hit;  
Hit.Trace(Vector1, Vector2, This);

```
		Vector1.Y += PlusX;
		Vector1.X -= PlusY;
		Vector2.Y += PlusX;
		Vector2.X -= PlusY;
		AsyncTask(ENamedThreads::GameThread, [=]()
		{
			Debug(Vector1, Vector2, Delta);
		});

```

//…

```
void GenerateCoversThread::Debug(FVector VectorA, FVector VectorB, float DeltaA)
{
	UE_LOG(LogTemp, Warning, TEXT("Debuging"));
	UE_LOG(LogTemp, Warning, TEXT("Delta: %f"), DeltaA)
	DrawDebugSphere(This->GetWorld(), VectorA, 25.f, 12, FColor::Red, false, 5.f, 0, 1.f);
	DrawDebugSphere(This->GetWorld(), VectorB, 25.f, 12, FColor::Red , false, 5.f, 0, 1.f);
	return;
}

```

As you can see Delta changes value, without me telling it to do so

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [February 8, 2017, 2:34pm UTC](https://forums.unrealengine.com/t/how-to-multi-thread-of-wait-for-new-tick/381370/6 "2017-02-08T14:34:30Z")

</div>

I found a workaround, I tell the thread in its Run fnction to wait untill Delta is changed, and set my variables in game thread from the class calling this thread

And the performance increase was amazing btw, from a drop to bellow 5 fps to staying 100+ fps. Which I ussually have.
