# Could I get some help with a FRunnable and FRunnableThread problem?

**URL:** <https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [May 19, 2014, 6:09pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580 "2014-05-19T18:09:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_4537ecff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4537ecff](https://forums.unrealengine.com/u/anonymous_user_4537ecff)\
**Post date:** [May 19, 2014, 6:09pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/1 "2014-05-19T18:09:57Z")

</div>

Hello!

I am trying to run a simple for loop in a separate thread using frunnable and frunnablethread, however each time i do - the editor freezes.

I am attempting to call this from a custom blueprint node.

I have attempted to follow examples in the engine source with no luck, i wondered if anyone could point out any issues.

Thanks!

Header:

```
#pragma once

#include "GameFramework/Actor.h"
#include <fstream>
#include "noise/noise.h"
#include "noiseutils.h"

class FPlanetBuilder : public FRunnable{

public:

	/**
	* Creates and initializes a new instance.
	*/
	FPlanetBuilder(bool run);
	/**
	* Destructor.
	*/
	~FPlanetBuilder();

public:

	// Begin FRunnable Interface

	virtual bool Init() OVERRIDE
	{
		return true;
	}

	virtual uint32 Run() OVERRIDE;

	virtual void Stop() OVERRIDE
	{
		bNeedsToStop = true;
	}

	virtual void Exit() OVERRIDE;

	// End FRunnable Interface

public:
 

	virtual void Shutdown()
	{
		Stop();
	}

	// End INetworkFileServer interface

private:
 
	// Holds the server thread object.
	FRunnableThread* PBThread;
 
	// Holds a flag indicating whether the thread should stop executing.
	bool bNeedsToStop;

	bool FPlanetBuilder::BuildPlanet();
};

```

CPP:

```
        #include "VicortisBP.h"
        #include "FPlanetBuilder.h"
        //#include "LNWorldGenerator.h"
        
        #include <fstream>
        
        #include "noise/noise.h"
        
        #include "noiseutils.h"
        using namespace noise;
        
        
          module::Cache PlanetCache;
        
         
        FPlanetBuilder::FPlanetBuilder(bool run):bNeedsToStop(false)
        {
        	bNeedsToStop = false;
        
        	FPlatformProcess::Sleep(0.25f);
        	if (run){
        		PBThread = FRunnableThread::Create(this, TEXT("FPlanetBuilder"), false, false, 8 * 1024, TPri_AboveNormal);
        	}
        }
        FPlanetBuilder::~FPlanetBuilder()
        {							
        	// Kill the running thread.
        	if (PBThread != NULL)
        	{
        		PBThread->Kill(true);
        
        		delete PBThread;
        		PBThread = NULL;
        	}
        }
        
        uint32 FPlanetBuilder::Run()
        {
        	//// go until requested to be done
        	while (true)
        	{
        	//	//Do Stuff// 
        		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("TICK"));
        
        
        		FPlatformProcess::Sleep(0.25f);
        	}
        
        	return 0;
        }
        
        void FPlanetBuilder::Exit()
        {
         
        
        
        }
        
        bool FPlanetBuilder::BuildPlanet(){
        
        	////////////////////////////////////////////////////////////////////////////
        
        	return true;
        }

```

---

<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:** [May 19, 2014, 10:16pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/2 "2014-05-19T22:16:58Z")

</div>

#Dont use GEngine

You should not involve UObject’s in multi-threading, it’s not safe. (the Engine class is UObject)

More specifically, do not create/delete/modify UObjects in other threads!

**Do basic data type manipulations only, and send the results back to the main game / application thread.**

Rama

---

<div class="post-metadata">

**Author:** ![silentkratos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@silentkratos](https://forums.unrealengine.com/u/silentkratos)\
**Post date:** [March 7, 2015, 10:14am UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/3 "2015-03-07T10:14:45Z")

</div>

Did it work for you? Eventually?  
I am trying to do something similar to you.

---

<div class="post-metadata">

**Author:** ![silentkratos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@silentkratos](https://forums.unrealengine.com/u/silentkratos)\
**Post date:** [March 7, 2015, 10:16am UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/4 "2015-03-07T10:16:12Z")

</div>

Hey @Rama, I tried to do as you said.  
I even followed your article here.

> **[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...

But It is not working.  
I am calling the JoyInit() static function from my gamemode.cpp file.  
Can you help?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4537ecff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4537ecff](https://forums.unrealengine.com/u/anonymous_user_4537ecff)\
**Post date:** [March 10, 2015, 8:28pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/5 "2015-03-10T20:28:09Z")

</div>

Nope, i found that the changes between engine versions are often breaking and that most examples for C++ dont work. I decided against sticking with an old version as so many new features and improvements are released each time.

Rama’s tutorials are super helpful buti find myself often frustrated with all the extra fluff, when i just want simple threading like std::thread.

With that in mind, and the fact that visual studio’s intellisense is appalling when working with UE4 (worse with source) even on a high end system with SSD’s i decided to cancel my subscription and try out Unity3d.

Until there’s a clear sources of documentation for each release with working c++ examples i wont be returning. Epic seems to be focusing on the blueprint faff.

\*\*Im told that there are third party plugins that solve the code completion problem but they cost more than the engine did itself (when it wasnt free).

---

<div class="post-metadata">

**Author:** ![silentkratos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@silentkratos](https://forums.unrealengine.com/u/silentkratos)\
**Post date:** [March 10, 2015, 9:05pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/6 "2015-03-10T21:05:57Z")

</div>

> **[MultiThreading in UE4?](https://forums.unrealengine.com/questions/186183/multithreading-in-ue4.html)**
>
> I tried using Rama’s Tutorial: There’s no compile error. But the thread’s Run() function is not executed when I run the game, I don’t know why. Can you help me ? GameMode.cpp: ARoboRev\_WinGameMode::ARoboRev\_WinGameMode(const...

---

<div class="post-metadata">

**Author:** ![silentkratos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@silentkratos](https://forums.unrealengine.com/u/silentkratos)\
**Post date:** [March 10, 2015, 9:06pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/7 "2015-03-10T21:06:27Z")

</div>

try this. i posted a question and rama replied to it. i m doing a simple thread thing too.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a4cc7aca](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a4cc7aca](https://forums.unrealengine.com/u/anonymous_user_a4cc7aca)\
**Post date:** [September 28, 2015, 7:56pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/8 "2015-09-28T19:56:14Z")

</div>

I’m using UObjects In threads just fine. You just have to pass them back to the game thread in a thread safe way.

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [October 5, 2015, 9:09am UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/9 "2015-10-05T09:09:54Z")

</div>

But did rama’s suggestion work for you ?

PS : I have the same issue. I have noticed that it does not happen in debug mode for me. and also the issue is with the Kill function.

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [October 5, 2015, 7:28pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/10 "2015-10-05T19:28:57Z")

</div>

I had the same issue and recently found out it happens because of PBThread-\>Kill(true); section  
**simple solution : in case you do not use any lock in your RUN() function then just simply call it with PBThread-\>Kill(false);**

problem is that within the Kill function of FrunnableThread there is an wait for infinity which in actually should never takes a long time after calling Close function. but strangely it stuck there

```
virtual bool Kill( bool bShouldWait = false ) override
	{
		check(Thread && "Did you forget to call Create()?");
		bool bDidExitOK = true;
		// Let the runnable have a chance to stop without brute force killing
		if (Runnable)
		{
			Runnable->Stop();
		}
		// If waiting was specified, wait the amount of time. If that fails,
		// brute force kill that thread. Very bad as that might leak.
		if (bShouldWait == true)
		{
			// Wait indefinitely for the thread to finish. IMPORTANT: It's not safe to just go and
			// kill the thread with TerminateThread() as it could have a mutex lock that's shared
			// with a thread that's continuing to run, which would cause that other thread to
			// dead-lock. (This can manifest itself in code as simple as the synchronization
			// object that is used by our logging output classes. Trust us, we've seen it!)
			WaitForSingleObject(Thread,INFINITE);
		}
		// Now clean up the thread handle so we don't leak
		CloseHandle(Thread);
		Thread = NULL;

		return bDidExitOK;
	}
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_69b87708](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_69b87708](https://forums.unrealengine.com/u/anonymous_user_69b87708)\
**Post date:** [January 26, 2016, 11:55am UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/11 "2016-01-26T11:55:30Z")

</div>

I agree with everything you have said. It is also a shame that it is so very difficult to get any replies on this answers site. Unfortunately, I have to stick with UE4 for my PhD…

---

<div class="post-metadata">

**Author:** ![Vertex\_Soup](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vertex\_Soup](https://forums.unrealengine.com/u/Vertex_Soup)\
**Post date:** [April 2, 2017, 4:53pm UTC](https://forums.unrealengine.com/t/could-i-get-some-help-with-a-frunnable-and-frunnablethread-problem/283580/12 "2017-04-02T16:53:17Z")

</div>

Can you ellaborate with some simple example? I’m trying to wrap my head around this… Thank You.
