Skip to content

Instantly share code, notes, and snippets.

@juaxix
Last active November 15, 2021 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juaxix/1f153eb46748214947523d8517d17291 to your computer and use it in GitHub Desktop.
Save juaxix/1f153eb46748214947523d8517d17291 to your computer and use it in GitHub Desktop.
Unreal Background Runnable thread
/**
* Class to run a background thread do parallel work with different Objects using an interface
*/
class FBackgroundRunnable : public FRunnable
{
public:
explicit FBackgroundRunnable(IMediator* InObject)
{
Object = InObject;
BackgroundThread = FRunnableThread::Create(this, TEXT("Background Thread"), 0, EThreadPriority::TPri_Highest, FPlatformAffinity::GetAsyncLoadingThreadMask());
}
virtual ~FBackgroundRunnable() override
{
if (BackgroundThread)
{
BackgroundThread->Kill();
delete BackgroundThread;
}
Object = nullptr;
}
// FRunnable interface Begin
virtual bool Init() override
{
UE_LOG(LogTemp, Warning, TEXT("Background Thread has been initialized"));
return true;
}
virtual uint32 Run() override
{
while (Object)
{
Object->RunParallelComputation();
FPlatformProcess::Sleep(BACKGROUND_RUNNABLE_DELAY);
}
return 0;
}
virtual void Stop() override { Object = nullptr; }
// FRunnable interface End
IMediator* Object = nullptr;
FRunnableThread* BackgroundThread = nullptr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment