On FRunnable and Rama's article

I’m trying to build a motion control system using OpenCV, but since it’s quite slow to use on the main thread I wanted to run the whole OpenCV code on a different thread.
I’ve been following Rama’s article about multithreading using FRunnable and FRunnableThread, but I must be doing something wrong because it looks like the thread I’m creating is running on the game thread. If I put an infinite loop inside the Run function of the Runnable class it freezes the whole game, if I put any code inside the Run function it first gets executed and then the game starts.
I can’t help but feel I’m missing something here.

I start the thread inside the player controller’s BeginPlay.

void ACustomPlayerController::BeginPlay()
{
	Super::BeginPlay();
        
        //Other stuff

	FWebcamRunnableObject::JoyInit(CursorPosition, FVector2D(x, y), this);
}

And shutdown it inside the EndPlay

void ACustomPlayerController::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	FWebcamRunnableObject::Shutdown();

	Super::EndPlay(EndPlayReason);
}

JoyInit does exactly what’s in Rama’s article

FWebcamRunnableObject* FWebcamRunnableObject::JoyInit(FVector2D& OUT_cursorPos, FVector2D viewportSize, ACustomPlayerController* IN_playerController)
{
	//Create new instance of thread if it does not exist
	//and the platform supports multi-threading

	if (!Runnable && FPlatformProcess::SupportsMultithreading())
	{
		Runnable = new FWebcamRunnableObject(OUT_cursorPos, viewportSize, IN_playerController);

		Runnable->Init();
	}

	return Runnable;
}

Anyone has any idea on what I’m doing wrong?

I found the issue.
From the FRunnable documentation page

A runnable object is an object that is “run” on an arbitrary thread. The call usage pattern is Init() , Run() , Exit() . The thread that is going to “run” this object always uses those calling semantics.

And from the Init() declaration

This method is called in the context of the thread object that aggregates this, not the thread that passes this runnable to a new thread.

I was calling the Runnable Init() function inside the static initialization function, which is inside the player controller. The player controller is on the main thread, therefore calling Init() inside the main thread will start the new thread inside the main thread. Basically I was not multi-threading at all. That’s why my main thread was stalling.

The Init() function doesn’t need to be called manually because it’s already called inside the FRunnableThread::Run() function. And in that case, it’s already on a new thread.
That’s why deleting Init() solved my issue.

At least that’s what I think was going on. Hope I’m not mistaken and hope this helps other people! :slight_smile: