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)
99 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
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;
123 *spIsRunning = {
true};
126 catch (
const std::exception& e)
128 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
132 template<
typename TDatums,
typename TWorker>
139 *spIsRunning = {
true};
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__);
237 #endif // OPENPOSE_THREAD_THREAD_HPP
void startInThread()
Definition: thread.hpp:133
Definition: subThread.hpp:10
void add(const std::vector< std::shared_ptr< SubThread< TDatums, TWorker >>> &subThreads)
Definition: thread.hpp:104
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:89
void stopAndJoin()
Definition: thread.hpp:149
bool isRunning() const
Definition: thread.hpp:36
Thread & operator=(Thread &&t)
Definition: thread.hpp:80
void exec(const std::shared_ptr< std::atomic< bool >> &isRunningSharedPtr)
Definition: thread.hpp:117
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:12
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:66