1 #ifndef OPENPOSE_THREAD_THREAD_HPP
2 #define OPENPOSE_THREAD_THREAD_HPP
12 template<
typename TDatums,
typename TWorker = std::shared_ptr<Worker<TDatums>>>
16 explicit Thread(
const std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr =
nullptr);
31 void exec(
const std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr);
43 std::shared_ptr<std::atomic<bool>> spIsRunning;
44 std::vector<std::shared_ptr<SubThread<TDatums, TWorker>>> mSubThreads;
47 void initializationOnThread();
49 void threadFunction();
66 template<
typename TDatums,
typename TWorker>
68 spIsRunning{(isRunningSharedPtr !=
nullptr ? isRunningSharedPtr : std::make_shared<std::atomic<bool>>(
false))}
72 template<
typename TDatums,
typename TWorker>
74 spIsRunning{std::make_shared<std::atomic<bool>>(t.spIsRunning->load())}
76 std::swap(mSubThreads, t.mSubThreads);
77 std::swap(mThread, t.mThread);
80 template<
typename TDatums,
typename TWorker>
83 std::swap(mSubThreads, t.mSubThreads);
84 std::swap(mThread, t.mThread);
85 spIsRunning = {std::make_shared<std::atomic<bool>>(t.spIsRunning->load())};
89 template<
typename TDatums,
typename TWorker>
98 catch (
const std::exception& e)
100 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
104 template<
typename TDatums,
typename TWorker>
107 for (
const auto& subThread : subThreads)
108 mSubThreads.emplace_back(subThread);
111 template<
typename TDatums,
typename TWorker>
117 template<
typename TDatums,
typename TWorker>
123 spIsRunning = isRunningSharedPtr;
124 *spIsRunning = {
true};
127 catch (
const std::exception& e)
129 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
133 template<
typename TDatums,
typename TWorker>
140 *spIsRunning = {
true};
141 mThread = {std::thread{&Thread::threadFunction,
this}};
143 catch (
const std::exception& e)
145 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
149 template<
typename TDatums,
typename TWorker>
157 catch (
const std::exception& e)
159 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
163 template<
typename TDatums,
typename TWorker>
169 for (
auto& subThread : mSubThreads)
170 subThread->initializationOnThread();
172 catch (
const std::exception& e)
174 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
178 template<
typename TDatums,
typename TWorker>
179 void Thread<TDatums, TWorker>::threadFunction()
184 initializationOnThread();
189 bool allSubThreadsClosed =
true;
190 for (
auto& subThread : mSubThreads)
191 allSubThreadsClosed &= !subThread->work();
193 if (allSubThreadsClosed)
202 catch (
const std::exception& e)
204 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
208 template<
typename TDatums,
typename TWorker>
209 void Thread<TDatums, TWorker>::stop()
213 *spIsRunning = {
false};
215 catch (
const std::exception& e)
217 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
221 template<
typename TDatums,
typename TWorker>
222 void Thread<TDatums, TWorker>::join()
226 if (mThread.joinable())
229 catch (
const std::exception& e)
231 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
238 #endif // OPENPOSE_THREAD_THREAD_HPP
void startInThread()
Definition: thread.hpp:134
Definition: subThread.hpp:10
void add(const std::vector< std::shared_ptr< SubThread< TDatums, TWorker >>> &subThreads)
Definition: thread.hpp:105
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
virtual ~Thread()
Definition: thread.hpp:90
void stopAndJoin()
Definition: thread.hpp:150
bool isRunning() const
Definition: thread.hpp:37
Thread & operator=(Thread &&t)
Definition: thread.hpp:81
void exec(const std::shared_ptr< std::atomic< bool >> &isRunningSharedPtr)
Definition: thread.hpp:118
OP_API void log(const std::string &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")
Definition: thread.hpp:13
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)
std::vector< T, Alloc > vector
Definition: cl2.hpp:567
Thread(const std::shared_ptr< std::atomic< bool >> &isRunningSharedPtr=nullptr)
Definition: thread.hpp:67