Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.h
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- /**
- *
- */
- class THREADING_API TestThread : public FRunnable
- {/** Singleton instance, can access the thread any time via static accessor, if it is active! */
- static TestThread* Runnable;
- /** Thread to run the worker FRunnable on */
- FRunnableThread* Thread;
- private:
- bool Running = false;
- bool Paused = false;
- public:
- //Example of how you can access data.
- uint32 i = 0;
- //~~~ Thread Core Functions ~~~
- //Constructor / Destructor
- TestThread();
- virtual ~TestThread();
- // Begin FRunnable interface. DONT call these yourself.
- 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 ~~~
- void StopThread();
- void PauseThread();
- void ResumeThread();
- /*
- 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 TestThread* CreateThread();
- /** Shuts down the thread. Static so it can easily be called from outside the thread context */
- static void Shutdown();
- static bool IsThreadFinished();
- };
- //.cpp
- #include "Threading.h"
- #include "TestThread.h"
- //***********************************************************
- //Thread Worker Starts as NULL, prior to being instanced
- // This line is essential! Compiler error without it
- TestThread* TestThread::Runnable = NULL;
- //***********************************************************
- TestThread::TestThread()
- {
- //Link to where data should be stored
- Thread = FRunnableThread::Create(this, TEXT("TestThread"), 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more
- }
- TestThread::~TestThread()
- {
- delete Thread;
- Thread = NULL;
- }
- //Init
- bool TestThread::Init()
- {
- //Init the Data
- Running = true;
- return true;
- }
- void TestThread::StopThread()
- {
- Running = false;
- }
- void TestThread::PauseThread()
- {
- Paused = true;
- }
- void TestThread::ResumeThread()
- {
- Paused = false;
- }
- //Run
- uint32 TestThread::Run()
- {
- //Initial wait before starting
- FPlatformProcess::Sleep(0.03);
- //While not told to stop this thread
- // and not yet finished finding Prime Numbers
- while (Running) {
- i++;
- //Check for connections
- FPlatformProcess::Sleep(0.03);
- //Check for data
- FPlatformProcess::Sleep(0.03);
- }
- return 0;
- }
- //stop
- void TestThread::Stop()
- {
- Running = false;
- }
- TestThread* TestThread::CreateThread()
- {
- //Create new instance of thread if it does not exist
- // and the platform supports multi threading!
- if (!Runnable && FPlatformProcess::SupportsMultithreading()) {
- Runnable = new TestThread();
- }
- return Runnable;
- }
- void TestThread::EnsureCompletion()
- {
- Stop();
- Thread->WaitForCompletion();
- }
- void TestThread::Shutdown()
- {
- if (Runnable) {
- Runnable->EnsureCompletion();
- delete Runnable;
- Runnable = NULL;
- }
- }
- bool TestThread::IsThreadFinished()
- {
- if (Runnable) return Runnable->Running;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement