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();
};
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: