OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
subThread.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_THREAD_SUB_THREAD_HPP
2 #define OPENPOSE_THREAD_SUB_THREAD_HPP
3 
6 
7 namespace op
8 {
9  template<typename TDatums, typename TWorker = std::shared_ptr<Worker<TDatums>>>
10  class SubThread
11  {
12  public:
13  explicit SubThread(const std::vector<TWorker>& tWorkers);
14 
15  // Destructor
16  virtual ~SubThread();
17 
19 
20  virtual bool work() = 0;
21 
22  protected:
23  inline size_t getTWorkersSize() const
24  {
25  return mTWorkers.size();
26  }
27 
28  bool workTWorkers(TDatums& tDatums, const bool inputIsRunning);
29 
30  private:
31  std::vector<TWorker> mTWorkers;
32 
33  DELETE_COPY(SubThread);
34  };
35 }
36 
37 
38 
39 
40 
41 // Implementation
42 namespace op
43 {
44  template<typename TDatums, typename TWorker>
45  SubThread<TDatums, TWorker>::SubThread(const std::vector<TWorker>& tWorkers) :
46  mTWorkers{tWorkers}
47  {
48  }
49 
50  template<typename TDatums, typename TWorker>
52  {
53  }
54 
55  template<typename TDatums, typename TWorker>
56  bool SubThread<TDatums, TWorker>::workTWorkers(TDatums& tDatums, const bool inputIsRunning)
57  {
58  try
59  {
60  // If !inputIsRunning -> try to close TWorkers
61  if (!inputIsRunning)
62  {
63  for (auto& tWorkers : mTWorkers)
64  {
65  tWorkers->tryStop();
66  if (tWorkers->isRunning())
67  break;
68  }
69  }
70 
71  // If (at least) last TWorker still working -> make TWorkers work
72  if ((*mTWorkers.crbegin())->isRunning())
73  {
74  // Iterate over all workers and check whether some of them stopped
75  auto allRunning = true;
76  auto lastOneStopped = false;
77  for (auto& worker : mTWorkers)
78  {
79  if (lastOneStopped)
80  worker->tryStop();
81 
82  if (!worker->checkAndWork(tDatums))
83  {
84  allRunning = false;
85  lastOneStopped = true;
86  }
87  else
88  lastOneStopped = false;
89  }
90 
91  if (allRunning)
92  return true;
93  else
94  {
95  // If last one still running -> try to stop workers
96  // If last one stopped -> return false
97  auto lastRunning = (*mTWorkers.crbegin())->isRunning();
98  if (lastRunning)
99  {
100  // Check last one that stopped
101  auto lastIndexNotRunning = 0ull;
102  for (auto i = mTWorkers.size() - 1 ; i > 0 ; i--)
103  {
104  if (!mTWorkers[i]->checkAndWork(tDatums))
105  {
106  lastIndexNotRunning = i;
107  break;
108  }
109  }
110 
111  // Stop workers before last index stopped
112  for (auto i = 0ull; i < lastIndexNotRunning ; i++)
113  mTWorkers[i]->stop();
114 
115  // Try stopping workers after last index stopped
116  lastRunning = false;
117  for (auto i = lastIndexNotRunning+1; i < mTWorkers.size() ; i++)
118  {
119  mTWorkers[i]->tryStop();
120  if (mTWorkers[i]->isRunning())
121  {
122  lastRunning = true;
123  break;
124  }
125  }
126  }
127  return lastRunning;
128  }
129  }
130  else
131  return false;
132  }
133  catch (const std::exception& e)
134  {
135  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
136  return false;
137  }
138  }
139 
140  template<typename TDatums, typename TWorker>
142  {
143  try
144  {
145  for (auto& tWorker : mTWorkers)
146  tWorker->initializationOnThreadNoException();
147  }
148  catch (const std::exception& e)
149  {
150  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
151  }
152  }
153 
155 }
156 
157 #endif // OPENPOSE_THREAD_SUB_THREAD_HPP
virtual bool work()=0
size_t getTWorkersSize() const
Definition: subThread.hpp:23
virtual ~SubThread()
Definition: subThread.hpp:51
void initializationOnThread()
Definition: subThread.hpp:141
SubThread(const std::vector< TWorker > &tWorkers)
Definition: subThread.hpp:45
bool workTWorkers(TDatums &tDatums, const bool inputIsRunning)
Definition: subThread.hpp:56
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")