? 久久精品无码一区二区一不,真实强奷在线中文,成年网站未满十八禁视频天堂
聯(lián)系我們

給我們留言

聯(lián)系我們

地址:福建省晉江市青陽街道洪山路國際工業(yè)設(shè)計園納金網(wǎng)

郵箱:info@narkii.com

電話:0595-82682267

(周一到周五, 周六周日休息)

當前位置:主頁 > 3D教程 > 圖文教程

E4][multi-thread]如何創(chuàng)建和終止線程:FRunnableThrea

來源: 52vr | 責(zé)任編輯:傳說的落葉 | 發(fā)布時間: 2019-06-06 08:55 | 瀏覽量:

[UE4][multi-thread]如何創(chuàng)建和終止線程:FRunnableThread::Create()

 

原文: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頭文件:

 
  1. //~~~~~ Multi Threading ~~~  
  2. class FPrimeNumberWorker : public FRunnable  
  3. {     
  4.     /** Singleton instance, can access the thread any time via static accessor, if it is active! */  
  5.     static  FPrimeNumberWorker* Runnable;  
  6.    
  7.     /** Thread to run the worker FRunnable on */  
  8.     FRunnableThread* Thread;  
  9.    
  10.     /** The Data Ptr */  
  11.     TArray<uint32>* PrimeNumbers;  
  12.    
  13.     /** The PC */  
  14.     AVictoryGamePlayerController* ThePC;  
  15.    
  16.     /** Stop this thread? Uses Thread Safe Counter */  
  17.     FThreadSafeCounter StopTaskCounter;  
  18.    
  19.     //The actual finding of prime numbers  
  20.     int32 FindNextPrimeNumber();  
  21.    
  22. private:  
  23.     int32               PrimesFoundCount;  
  24. public:  
  25.    
  26.     int32               TotalPrimesToFind;  
  27.    
  28.     //Done?  
  29.     bool IsFinished() const  
  30.     {  
  31.         return PrimesFoundCount >= TotalPrimesToFind;  
  32.     }  
  33.    
  34.     //~~~ Thread Core Functions ~~~  
  35.    
  36.     //Constructor / Destructor  
  37.     FPrimeNumberWorker(TArray<uint32>& TheArray, const int32 IN_PrimesToFindPerTick, AVictoryGamePlayerController* IN_PC);  
  38.     virtual ~FPrimeNumberWorker();  
  39.    
  40.     // Begin FRunnable interface.  
  41.     virtual bool Init();  
  42.     virtual uint32 Run();  
  43.     virtual void Stop();  
  44.     // End FRunnable interface  
  45.    
  46.     /** Makes sure this thread has stopped properly */  
  47.     void EnsureCompletion();  
  48.    
  49.    
  50.    
  51.     //~~~ Starting and Stopping Thread ~~~  
  52.    
  53.    
  54.    
  55.     /*  
  56.         Start the thread and the worker from static (easy access)!  
  57.         This code ensures only 1 Prime Number thread will be able to run at a time.  
  58.         This function returns a handle to the newly started instance. 
  59.     */  
  60.     static FPrimeNumberWorker* JoyInit(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC);  
  61.    
  62.     /** Shuts down the thread. Static so it can easily be called from outside the thread context */  
  63.     static void Shutdown();  
  64.    
  65.     static bool IsThreadFinished();  
  66. };  

 

cpp文件:

 
  1. //***********************************************************  
  2. //Thread Worker Starts as NULL, prior to being instanced  
  3. //      This line is essential! Compiler error without it  
  4. FPrimeNumberWorker* FPrimeNumberWorker::Runnable = NULL;  
  5. //***********************************************************  
  6.    
  7. FPrimeNumberWorker::FPrimeNumberWorker(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC)  
  8.     : ThePC(IN_PC)  
  9.     , TotalPrimesToFind(IN_TotalPrimesToFind)  
  10.     , StopTaskCounter(0)  
  11.     , PrimesFoundCount(0)  
  12. {  
  13.     //Link to where data should be stored  
  14.     PrimeNumbers = &TheArray;  
  15.   
  16.     Thread = FRunnableThread::Create(this, TEXT("FPrimeNumberWorker"), 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more  
  17. }  
  18.    
  19. FPrimeNumberWorker::~FPrimeNumberWorker()  
  20. {  
  21.     delete Thread;  
  22.     Thread = NULL;  
  23. }  
  24.    
  25. //Init  
  26. bool FPrimeNumberWorker::Init()  
  27. {  
  28.     //Init the Data   
  29.     PrimeNumbers->Empty();  
  30.     PrimeNumbers->Add(2);  
  31.     PrimeNumbers->Add(3);  
  32.    
  33.     if(ThePC)   
  34.     {  
  35.         ThePC->ClientMessage("**********************************");  
  36.         ThePC->ClientMessage("Prime Number Thread Started!");  
  37.         ThePC->ClientMessage("**********************************");  
  38.     }  
  39.     return true;  
  40. }  
  41.    
  42. //Run  
  43. uint32 FPrimeNumberWorker::Run()  
  44. {  
  45.     //Initial wait before starting  
  46.     FPlatformProcess::Sleep(0.03);  
  47.    
  48.     //While not told to stop this thread   
  49.     //      and not yet finished finding Prime Numbers  
  50.     while (StopTaskCounter.GetValue() == 0 && ! IsFinished())  
  51.     {  
  52.         PrimeNumbers->Add(FindNextPrimeNumber());  
  53.         PrimesFoundCount++;  
  54.    
  55.         //***************************************  
  56.         //Show Incremental Results in Main Game Thread!  
  57.    
  58.         //  Please note you should not create, destroy, or modify UObjects here.  
  59.         //    Do those sort of things after all thread are completed.  
  60.    
  61.         //    All calcs for making stuff can be done in the threads  
  62.         //       But the actual making/modifying of the UObjects should be done in main game thread.  
  63.         ThePC->ClientMessage(FString::FromInt(PrimeNumbers->Last()));  
  64.         //***************************************  
  65.    
  66.         //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  67.         //prevent thread from using too many resources  
  68.         //FPlatformProcess::Sleep(0.01);  
  69.         //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  70.     }  
  71.    
  72.     //Run FPrimeNumberWorker::Shutdown() from the timer in Game Thread that is watching  
  73.         //to see when FPrimeNumberWorker::IsThreadFinished()  
  74.    
  75.     return 0;  
  76. }  
  77.    
  78. //stop  
  79. void FPrimeNumberWorker::Stop()  
  80. {  
  81.     StopTaskCounter.Increment();  
  82. }  
  83.    
  84. FPrimeNumberWorker* FPrimeNumberWorker::JoyInit(TArray<uint32>& TheArray, const int32 IN_TotalPrimesToFind, AVictoryGamePlayerController* IN_PC)  
  85. {  
  86.     //Create new instance of thread if it does not exist  
  87.     //      and the platform supports multi threading!  
  88.     if (!Runnable && FPlatformProcess::SupportsMultithreading())  
  89. <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高級,虛幻4技巧


相關(guān)文章
網(wǎng)友評論

您需要登錄后才可以發(fā)帖 登錄 | 立即注冊

關(guān)閉

全部評論:0條

推薦
熱門