Last active
November 15, 2021 09:58
-
-
Save juaxix/1f153eb46748214947523d8517d17291 to your computer and use it in GitHub Desktop.
Unreal Background Runnable thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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