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?