1 #ifndef OPENPOSE_THREAD_THREAD_HPP
2 #define OPENPOSE_THREAD_THREAD_HPP
11 template<
typename TDatums,
typename TWorker = std::shared_ptr<Worker<TDatums>>>
15 explicit Thread(
const std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr =
nullptr);
30 void exec(
const std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr);
42 std::shared_ptr<std::atomic<bool>> spIsRunning;
43 std::vector<std::shared_ptr<SubThread<TDatums, TWorker>>> mSubThreads;
46 void initializationOnThread();
48 void threadFunction();
65 template<
typename TDatums,
typename TWorker>
67 spIsRunning{(isRunningSharedPtr != nullptr ? isRunningSharedPtr : std::make_shared<std::atomic<bool>>(false))}
71 template<
typename TDatums,
typename TWorker>
73 spIsRunning{std::make_shared<std::atomic<bool>>(t.spIsRunning->load())}
75 std::swap(mSubThreads, t.mSubThreads);
76 std::swap(mThread, t.mThread);
79 template<
typename TDatums,
typename TWorker>
82 std::swap(mSubThreads, t.mSubThreads);
83 std::swap(mThread, t.mThread);
84 spIsRunning = {std::make_shared<std::atomic<bool>>(t.spIsRunning->load())};
88 template<
typename TDatums,
typename TWorker>
97 catch (
const std::exception& e)
103 template<
typename TDatums,
typename TWorker>
106 for (
const auto& subThread : subThreads)
107 mSubThreads.emplace_back(subThread);
110 template<
typename TDatums,
typename TWorker>
116 template<
typename TDatums,
typename TWorker>
122 spIsRunning = isRunningSharedPtr;
126 catch (
const std::exception& e)
128 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
132 template<
typename TDatums,
typename TWorker>
140 mThread = {std::thread{&Thread::threadFunction,
this}};
142 catch (
const std::exception& e)
144 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
148 template<
typename TDatums,
typename TWorker>
156 catch (
const std::exception& e)
158 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
162 template<
typename TDatums,
typename TWorker>
168 for (
auto& subThread : mSubThreads)
169 subThread->initializationOnThread();
171 catch (
const std::exception& e)
173 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
177 template<
typename TDatums,
typename TWorker>
178 void Thread<TDatums, TWorker>::threadFunction()
183 initializationOnThread();
188 bool allSubThreadsClosed =
true;
189 for (
auto& subThread : mSubThreads)
190 allSubThreadsClosed &= !subThread->work();
192 if (allSubThreadsClosed)
201 catch (
const std::exception& e)
203 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
207 template<
typename TDatums,
typename TWorker>
208 void Thread<TDatums, TWorker>::stop()
212 *spIsRunning =
false;
214 catch (
const std::exception& e)
216 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
220 template<
typename TDatums,
typename TWorker>
221 void Thread<TDatums, TWorker>::join()
225 if (mThread.joinable())
228 catch (
const std::exception& e)
230 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
void add(const std::vector< std::shared_ptr< SubThread< TDatums, TWorker >>> &subThreads)
Thread & operator=(Thread &&t)
Thread(const std::shared_ptr< std::atomic< bool >> &isRunningSharedPtr=nullptr)
void exec(const std::shared_ptr< std::atomic< bool >> &isRunningSharedPtr)
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
OP_API void errorDestructor(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
OP_API void opLog(const std::string &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")