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"

How to use the FRunnable?

I mean if I implement a thread class by FRunnable, how can I start up the thread? Should I call the Run() function or do some other things? The code demo is as follows:

FSocketListenThread.h:

 #pragma once
  
 class CONNECTPC_API FSocketListenThread : public FRunnable {
 
     /** Singleton instance, can access the thread any time via static accessor, if it is active! */
     static  FSocketListenThread* Instance;
 
     /** Thread to run the worker FRunnable on */
     FRunnableThread* Thread;
 
     /** Stop this thread? Uses Thread Safe Counter */
     FThreadSafeCounter StopTaskCounter;
 
     virtual uint32 Run();
     virtual void Stop();
     // End FRunnable interface
 
     /** Makes sure this thread has stopped properly */
     void EnsureCompletion();
 
 
     /** Shuts down the thread. Static so it can easily be called from outside the thread context */
     static void Shutdown();
 
     static bool IsThreadFinished();
 
 
 public:
     FSocketListenThread();
     ~FSocketListenThread();
 };


Product Version: UE 4.15
Tags:
more ▼

asked Apr 12 '17 at 11:41 AM in C++ Programming

avatar image

Yunze Gu
63 16 18 20

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Hi To run your thread you have to use

     Thread = FRunnableThread::Create(PointerToYourFSocketListenThreadInstance, TEXT("FSocketListenThreadRunnable - a name for thread dispatcher"), ThreadStackSize, TPri_AboveNormal);
 

This call gives you a WorkerThread handle which could be used to control your thread. You can Suspend, Kill or wait for completion of newly created thread.

 Thread->WaitForCompletion();  //Waits till thread is finished

Run and Stop functions of FRunnable will be called outside. "Run" should process things you want to do in a separate thread. "Stop" called after "Run" ends or when you killing this thread. You can use single class to manage multithreading and process your data. To do this create a function in your class and run something like this:

 bool StartListening()
 {
     if (Thread != nullptr) return false; //Already running
     Thread = FRunnableThread::Create(this, TEXT("FSocketListenThread"), FSocketListenThread::ThreadStackSize, TPri_AboveNormal);
     return (Thread != nullptr)
 }
 
 void StopListening()
 {
     if (Thread != nullptr) {
         // Ask your thread to stop somehow
         Thread->WaitForCompletion();
         delete Thread;
         Thread = nullptr;
     }
 }

Note that you dave to 'delete' thread handle if it was created successfully.

more ▼

answered Apr 12 '17 at 12:16 PM

avatar image

c4tnt
1k 12 8 30

avatar image Yunze Gu Apr 13 '17 at 06:06 AM

It works. Thank you very much!

avatar image SysOverdrive Apr 24 '18 at 09:28 AM

How do you get the Thread stack size ThreadStackSize?

avatar image Schnarek Oct 22 '18 at 02:56 PM

Hey Sys did you found out ?

(comments are locked)
10|2000 characters needed characters left
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