x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.17
Tags:
more ▼

asked Dec 02 '17 at 12:13 AM in C++ Programming

avatar image

EmilioLaiso
1 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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! :)

more ▼

answered Dec 02 '17 at 12:18 PM

avatar image

EmilioLaiso
1 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question