Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
InitiallyPausedThread.h
Go to the documentation of this file.
1#pragma once
2
3#include "Utility/IMoveOnly.h"
4
5#include <Common/debug.h>
6#include <Common/exceptions.h>
7#include <Common/logging.h>
8
9#include <thread>
10#include <future>
11#include <functional>
12#include <utility>
13
14namespace ph
15{
16
17class InitiallyPausedThread final : private IMoveOnly
18{
19public:
23
25
34 template<typename Func, typename... Args>
35 explicit InitiallyPausedThread(Func&& func, Args&&... args);
36
41
46 void start();
47
53 void join();
54
58 bool hasStarted() const;
59
62 bool hasJoined() const;
63
67 std::thread::id getId() const;
68
70
71private:
72 void setPromisedValue(bool value);
73 bool isEmptyThread() const;
74
75 std::thread m_thread;
76 std::promise<bool> m_startPromise;
77 bool m_hasStarted;
78 bool m_hasJoined;
79};
80
81template<typename Func, typename... Args>
82inline InitiallyPausedThread::InitiallyPausedThread(Func&& func, Args&&... args)
84{
85 m_thread = std::thread(
86 [startFuture = m_startPromise.get_future(),
87 func = std::bind(std::forward<Func>(func), std::forward<Args>(args)...)]
88 () mutable
89 {
90 try
91 {
92 if(startFuture.get())
93 {
94 func();
95 }
96 }
97 catch(const Exception& e)
98 {
99 PH_DEFAULT_LOG(Error,
100 "[InitiallyPausedThread] unhandled exception thrown: {}", e.what());
101
102 PH_DEBUG_BREAK();
103 }
104 });
105}
106
107inline bool InitiallyPausedThread::hasStarted() const
108{
109 return m_hasStarted;
110}
111
112inline bool InitiallyPausedThread::hasJoined() const
113{
114 return m_hasJoined;
115}
116
117inline std::thread::id InitiallyPausedThread::getId() const
118{
119 return m_thread.get_id();
120}
121
122inline bool InitiallyPausedThread::isEmptyThread() const
123{
124 return m_thread.get_id() == std::thread::id();
125}
126
127}// end namespace ph
Marks the derived class as move only.
Definition IMoveOnly.h:23
Definition InitiallyPausedThread.h:18
std::thread::id getId() const
Get ID of the underlying thread.
Definition InitiallyPausedThread.h:117
bool hasStarted() const
Whether the thread has started executing the functor. The started state is visible to the body of the...
Definition InitiallyPausedThread.h:107
bool hasJoined() const
Whether the thread has joined.
Definition InitiallyPausedThread.h:112
~InitiallyPausedThread()
Definition InitiallyPausedThread.cpp:64
InitiallyPausedThread & operator=(InitiallyPausedThread &&rhs)=default
void join()
Wait until the execution of the thread is finished. hasStarted() must be true when calling this metho...
Definition InitiallyPausedThread.cpp:34
void start()
Start the execution of the thread. This method synchronizes-with the start of the call to the functor...
Definition InitiallyPausedThread.cpp:18
InitiallyPausedThread()
An empty object that does not represent a thread.
Definition InitiallyPausedThread.cpp:11
InitiallyPausedThread(InitiallyPausedThread &&other)=default
The root for all renderer implementations.
Definition EEngineProject.h:6