E4][multi-thread]如何創(chuàng)建和終止線程:FRunnableThrea
來(lái)源:
52vr |
責(zé)任編輯:傳說(shuō)的落葉 |
發(fā)布時(shí)間: 2019-06-06 08:55 | 瀏覽量:
原文:https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4
相關(guān)文檔
FRunnable
https://docs.unrealengine.com/latest/INT/API/Runtime/Core/HAL/FRunnable/index.html
FRunnableThread
https://docs.unrealengine.com/latest/INT/API/Runtime/Core/HAL/FRunnableThread/index.html
h頭文件:
- //~~~~~ Multi Threading ~~~
- class FPrimeNumberWorker : public FRunnable
- {
- /** Singleton instance, can access the thread any time via static accessor, if it is active! */
- static FPrimeNumberWorker* Runnable;
- /** Thread to run the worker FRunnable on */
- FRunnableThread* Thread;
- /** The Data Ptr */
- TArray<uint32>* PrimeNumbers;
- /** The PC */
- AVictoryGamePlayerController* ThePC;
- /** Stop this thread? Uses Thread Safe Counter */
- FThreadSafeCounter StopTaskCounter;
- //The actual finding of prime numbers
- int32 FindNextPrimeNumber();
- private:
- int32 PrimesFoundCount;
- public:
- int32 TotalPrimesToFind;
- //Done?
- bool IsFinished() const
- {
- return PrimesFoundCount >= TotalPrimesToFind;
- }
- //~~~ Thread Core Functions ~~~
- //Constructor / Destructor
- FPrimeNumberWorker(TArray<uint32>& TheArray, const int32 IN_PrimesToFindPerTick, AVictoryGamePlayerController* IN_PC);
- virtual ~FPrimeNumberWorker();
- // Begin FRunnable interface.
- virtual bool Init();
- virtual uint32 Run();
- virtual void Stop();
- // End FRunnable interface
- /** Makes sure this thread has stopped properly */
- void EnsureCompletion();
- //~~~ Starting and Stopping Thread ~~~
- /*
- Start the thread and the worker from static (easy access)!
- This code ensures only 1 Prime Number thread will be able to run at a time.
- This function returns a handle to the newly started instance.
- */
- static FPrimeNumberWorker* JoyInit(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC);
- /** Shuts down the thread. Static so it can easily be called from outside the thread context */
- static void Shutdown();
- static bool IsThreadFinished();
- };
cpp文件:
- //***********************************************************
- //Thread Worker Starts as NULL, prior to being instanced
- // This line is essential! Compiler error without it
- FPrimeNumberWorker* FPrimeNumberWorker::Runnable = NULL;
- //***********************************************************
- FPrimeNumberWorker::FPrimeNumberWorker(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC)
- : ThePC(IN_PC)
- , TotalPrimesToFind(IN_TotalPrimesToFind)
- , StopTaskCounter(0)
- , PrimesFoundCount(0)
- {
- //Link to where data should be stored
- PrimeNumbers = &TheArray;
- Thread = FRunnableThread::Create(this, TEXT("FPrimeNumberWorker"), 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more
- }
- FPrimeNumberWorker::~FPrimeNumberWorker()
- {
- delete Thread;
- Thread = NULL;
- }
- //Init
- bool FPrimeNumberWorker::Init()
- {
- //Init the Data
- PrimeNumbers->Empty();
- PrimeNumbers->Add(2);
- PrimeNumbers->Add(3);
- if(ThePC)
- {
- ThePC->ClientMessage("**********************************");
- ThePC->ClientMessage("Prime Number Thread Started!");
- ThePC->ClientMessage("**********************************");
- }
- return true;
- }
- //Run
- uint32 FPrimeNumberWorker::Run()
- {
- //Initial wait before starting
- FPlatformProcess::Sleep(0.03);
- //While not told to stop this thread
- // and not yet finished finding Prime Numbers
- while (StopTaskCounter.GetValue() == 0 && ! IsFinished())
- {
- PrimeNumbers->Add(FindNextPrimeNumber());
- PrimesFoundCount++;
- //***************************************
- //Show Incremental Results in Main Game Thread!
- // Please note you should not create, destroy, or modify UObjects here.
- // Do those sort of things after all thread are completed.
- // All calcs for making stuff can be done in the threads
- // But the actual making/modifying of the UObjects should be done in main game thread.
- ThePC->ClientMessage(FString::FromInt(PrimeNumbers->Last()));
- //***************************************
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //prevent thread from using too many resources
- //FPlatformProcess::Sleep(0.01);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
- //Run FPrimeNumberWorker::Shutdown() from the timer in Game Thread that is watching
- //to see when FPrimeNumberWorker::IsThreadFinished()
- return 0;
- }
- //stop
- void FPrimeNumberWorker::Stop()
- {
- StopTaskCounter.Increment();
- }
- FPrimeNumberWorker* FPrimeNumberWorker::JoyInit(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC)
- {
- //Create new instance of thread if it does not exist
- // and the platform supports multi threading!
- if (!Runnable && FPlatformProcess::SupportsMultithreading()) <li micxptag"="" style="overflow-wrap: break-word; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; font-size: 1em; border-left: 1px solid rgb(209, 215, 220); line-height: 18px;">虛幻4,ue4,虛幻4基礎(chǔ),虛幻4高級(jí),虛幻4技巧
-
分享到:
相關(guān)文章
網(wǎng)友評(píng)論
您需要登錄后才可以發(fā)帖 登錄 | 立即注冊(cè)
關(guān)閉
- 用戶名:
- 密 碼:
- 驗(yàn)證碼: 看不清? 點(diǎn)擊更換
- 忘記密碼?
全部評(píng)論:0條
推薦
熱門