Advertisement
Guest User

asdfasdfasd

a guest
Jul 28th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //.h
  2. // Fill out your copyright notice in the Description page of Project Settings.
  3.  
  4. #pragma once
  5.  
  6. /**
  7. *
  8. */
  9. class THREADING_API TestThread : public FRunnable
  10. {/** Singleton instance, can access the thread any time via static accessor, if it is active! */
  11. static TestThread* Runnable;
  12.  
  13. /** Thread to run the worker FRunnable on */
  14. FRunnableThread* Thread;
  15.  
  16. private:
  17. bool Running = false;
  18. bool Paused = false;
  19. public:
  20. //Example of how you can access data.
  21. uint32 i = 0;
  22.  
  23. //~~~ Thread Core Functions ~~~
  24.  
  25. //Constructor / Destructor
  26. TestThread();
  27. virtual ~TestThread();
  28.  
  29. // Begin FRunnable interface. DONT call these yourself.
  30. virtual bool Init();
  31. virtual uint32 Run();
  32. virtual void Stop();
  33. // End FRunnable interface
  34.  
  35. /** Makes sure this thread has stopped properly */
  36. void EnsureCompletion();
  37.  
  38.  
  39.  
  40. //~~~ Starting and Stopping Thread ~~~
  41.  
  42. void StopThread();
  43.  
  44. void PauseThread();
  45.  
  46. void ResumeThread();
  47.  
  48.  
  49. /*
  50. Start the thread and the worker from static (easy access)!
  51. This code ensures only 1 Prime Number thread will be able to run at a time.
  52. This function returns a handle to the newly started instance.
  53. */
  54. static TestThread* CreateThread();
  55.  
  56. /** Shuts down the thread. Static so it can easily be called from outside the thread context */
  57. static void Shutdown();
  58.  
  59. static bool IsThreadFinished();
  60. };
  61.  
  62.  
  63. //.cpp
  64.  
  65.  
  66. #include "Threading.h"
  67. #include "TestThread.h"
  68. //***********************************************************
  69. //Thread Worker Starts as NULL, prior to being instanced
  70. // This line is essential! Compiler error without it
  71. TestThread* TestThread::Runnable = NULL;
  72. //***********************************************************
  73.  
  74. TestThread::TestThread()
  75. {
  76. //Link to where data should be stored
  77.  
  78. Thread = FRunnableThread::Create(this, TEXT("TestThread"), 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more
  79. }
  80.  
  81. TestThread::~TestThread()
  82. {
  83. delete Thread;
  84. Thread = NULL;
  85. }
  86.  
  87. //Init
  88. bool TestThread::Init()
  89. {
  90. //Init the Data
  91. Running = true;
  92. return true;
  93. }
  94.  
  95. void TestThread::StopThread()
  96. {
  97. Running = false;
  98. }
  99.  
  100. void TestThread::PauseThread()
  101. {
  102. Paused = true;
  103. }
  104.  
  105. void TestThread::ResumeThread()
  106. {
  107. Paused = false;
  108. }
  109. //Run
  110. uint32 TestThread::Run()
  111. {
  112. //Initial wait before starting
  113. FPlatformProcess::Sleep(0.03);
  114.  
  115. //While not told to stop this thread
  116. // and not yet finished finding Prime Numbers
  117. while (Running) {
  118. i++;
  119. //Check for connections
  120. FPlatformProcess::Sleep(0.03);
  121. //Check for data
  122. FPlatformProcess::Sleep(0.03);
  123. }
  124.  
  125.  
  126. return 0;
  127. }
  128.  
  129. //stop
  130. void TestThread::Stop()
  131. {
  132. Running = false;
  133. }
  134.  
  135. TestThread* TestThread::CreateThread()
  136. {
  137. //Create new instance of thread if it does not exist
  138. // and the platform supports multi threading!
  139. if (!Runnable && FPlatformProcess::SupportsMultithreading()) {
  140. Runnable = new TestThread();
  141. }
  142. return Runnable;
  143. }
  144.  
  145. void TestThread::EnsureCompletion()
  146. {
  147. Stop();
  148. Thread->WaitForCompletion();
  149. }
  150.  
  151. void TestThread::Shutdown()
  152. {
  153. if (Runnable) {
  154. Runnable->EnsureCompletion();
  155. delete Runnable;
  156. Runnable = NULL;
  157. }
  158. }
  159.  
  160. bool TestThread::IsThreadFinished()
  161. {
  162. if (Runnable) return Runnable->Running;
  163. return true;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement