Faro Engine 0.0.0.b519570 (main)
Loading...
Searching...
No Matches
Thread.hpp
Go to the documentation of this file.
1#pragma once
2#include <thread>
3#include <ThreadSafety.hpp>
4#include <Primitives.hpp>
6#include <Math/Time.hpp>
8
9namespace Faro
10{
11 /// @brief Function type to be ran on another thread.
12 typedef Function<void()> ThreadTask;
13
14 /**
15 * @brief Sleep the calling thread by the given duration.
16 * @param duration Amount of time to sleep
17 */
18 extern void Sleep(Duration duration);
19
20 extern void RunOnThread(String threadId, ThreadTask task);
21
22 /// @brief Abstract interface of a thread.
24 {
25 public:
26 virtual ~IThreadInterface() = default;
27
28 /**
29 * @brief Schedule the supplied task to be executed ont this thread.
30 * @param task Task function to execute
31 */
32 void AddTask(ThreadTask task);
33
34 virtual String GetThreadId() = 0;
35
36 protected:
37 /// @brief Executed while starting the thread.
38 virtual void ThreadInit() = 0;
39 /// @brief Executed while the thread is active.
40 virtual void ThreadUpdate() = 0;
41 /// @brief Executed when the thread is being stopped.
42 virtual void ThreadDestroy() = 0;
43
44 /// @brief Executed any pending tasks.
45 void RunTasks();
46
47 void RegisterThread();
48 void UnregisterThread();
49
50 private:
52 };
53
54 /// @brief Base class for a thread.
56 {
57 public:
58 IThread() : shouldRun(true), isRunning(false)
59 {
60 }
61
62 /// @brief Start the thread.
63 void Start();
64
65 /// @brief Request the thread to stop.
66 void RequestStop();
67
68 /// @brief Request the thread to stop and wait for it.
69 void WaitForStop();
70
71 /**
72 * @brief Check if the thread is currently running.
73 * @return true The thread is running
74 * @return false The thread is not running
75 */
76 bool IsRunning();
77
78 String GetThreadId() override;
79
80 private:
81 void ThreadRun();
82
83 private:
84 std::thread thread;
85
86 ThreadSafe<bool> shouldRun;
87 ThreadSafe<bool> isRunning;
88 };
89}
Definition Time.hpp:8
Abstract interface of a thread.
Definition Thread.hpp:24
virtual void ThreadUpdate()=0
Executed while the thread is active.
virtual void ThreadInit()=0
Executed while starting the thread.
virtual String GetThreadId()=0
void RegisterThread()
Definition Thread.cpp:46
void RunTasks()
Executed any pending tasks.
Definition Thread.cpp:35
void AddTask(ThreadTask task)
Schedule the supplied task to be executed ont this thread.
Definition Thread.cpp:28
virtual ~IThreadInterface()=default
virtual void ThreadDestroy()=0
Executed when the thread is being stopped.
void UnregisterThread()
Definition Thread.cpp:53
Base class for a thread.
Definition Thread.hpp:56
void Start()
Start the thread.
Definition Thread.cpp:60
void RequestStop()
Request the thread to stop.
Definition Thread.cpp:68
String GetThreadId() override
Definition Thread.cpp:84
bool IsRunning()
Check if the thread is currently running.
Definition Thread.cpp:79
IThread()
Definition Thread.hpp:58
void WaitForStop()
Request the thread to stop and wait for it.
Definition Thread.cpp:73
Definition String.hpp:12
Container class providing thread-safe access to the internal object.
Definition ThreadSafety.hpp:24
Definition Array.hpp:8
void RunOnThread(String threadId, ThreadTask task)
Definition Thread.cpp:12
Function< void()> ThreadTask
Function type to be ran on another thread.
Definition Thread.hpp:12
std::function< T > Function
Definition Primitives.hpp:19
void Sleep(Duration duration)
Sleep the calling thread by the given duration.
Definition Thread.cpp:5