I have implemented threads for a custom plugin just yesterday, so here is an example.
I have not tested it yet though, so be warned!!
class ALTERNATEPHYSICS_API APPhysicsEngineThread : public FRunnable
{
public:
typedef std::shared_ptr<APPhysicsEngineThread> Pointer;
protected:
bool bShouldRun;
double timePrevious;
double timeCurrent;
float deltaTime;
public:
APPhysicsEngineThread();
void UpdateDeltaTime();
bool Init() override;
uint32 Run() override;
void Stop() override;
};
APPhysicsEngineThread::APPhysicsEngineThread()
{
}
bool APPhysicsEngineThread::Init()
{
bShouldRun = true;
timePrevious = FPlatformTime::Seconds();
timeCurrent = FPlatformTime::Seconds();
return true;
}
void APPhysicsEngineThread::UpdateDeltaTime()
{
timePrevious = timeCurrent;
timeCurrent = FPlatformTime::Seconds();
deltaTime = (float)(timeCurrent - timePrevious);
}
uint32 APPhysicsEngineThread::Run()
{
while (bShouldRun == true)
{
// just pause it a view milliseconds
FPlatformProcess::Sleep(0.005f);
}
FPlatformTime::Seconds();
return 0;
}
void APPhysicsEngineThread::Stop()
{
bShouldRun = false;
}
APPhysicsEngineThread::Pointer ptrPhysicsEngineThread = APPhysicsEngineThread::Pointer(new APPhysicsEngineThread());
FRunnableThread* ptrThread = FRunnableThread::Create(ptrPhysicsEngineThread.get(), TEXT("AlteratePhysicsThread"));