Create new Thread with FRunnable
cpp
// MyRunnalbe.h
#include "CoreMinimal.h"
#include "HAL/Runnable.h"
DECLARE_DYNAMIC( OnCompleteSignature );
class FMyRunnable : public FRunnable
{
public:
FMyRunnable();
virtual ~FMyRunnable();
public:
/**
* FRunnable functions
*/
virtual bool Init() override; // init thread
virtual uint32 Run() override; // run thread
virtual void Stop() override; // stop thread
virtual void Exit() override; // exit thread
/** On complete delegate */
OnCompleteSignature OnCompleteDelegate;
protected:
/** Thread to run the worker FRunnable on */
FRunnableThread* Thread = nullptr;
/** stop Thread? */
bool bIsFinished = false;
/** The Data Ptr */
int32* DataPtr;
};
cpp
// .cpp
#include "MyRunnable.h"
MyRunnable::MyRunnable()
{
Thread = FRunnableThread::Create(this, TEXT("MyRunnable"), 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more
// init
DataPtr = new int32(0);
}
MyRunnable::~MyRunnable()
{
delete DataPtr;
DataPtr = nullptr;
}
bool MyRunnable::Init()
{
return true;
}
uint32 MyRunnable::Run()
{
while (!bIsFinished)
{
// do something
}
return 0;
}
void MyRunnable::Stop()
{
bIsFinished = true;
}
void MyRunnable::Exit()
{
// do something
}
Running
cpp
// .cpp
// create runnable
MyRunnable* Runnable = new MyRunnable();
// start thread
Runnable->OnCompleteDelegate.BindLambda([Runnable]()
{
delete Runnable;
Runnable = nullptr;
});