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


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.



#pragma once

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

class FPlanetBuilder : public FRunnable{


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


	// 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


	virtual void Shutdown()

	// End INetworkFileServer interface

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

	bool FPlanetBuilder::BuildPlanet();


        #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;
        	if (run){
        		PBThread = FRunnableThread::Create(this, TEXT("FPlanetBuilder"), false, false, 8 * 1024, TPri_AboveNormal);
        	// Kill the running thread.
        	if (PBThread != NULL)
        		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"));
        	return 0;
        void FPlanetBuilder::Exit()
        bool FPlanetBuilder::BuildPlanet(){
        	return true;

#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.


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

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

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

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).

1 Like

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

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

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.

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)
		// 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!)
		// Now clean up the thread handle so we don't leak
		Thread = NULL;

		return bDidExitOK;

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…

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