OpenPose  1.0.0rc2
OpenPose: A Real-Time Multi-Person Key-Point Detection And Multi-Threading C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
subThreadQueueInOut.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_THREAD_THREAD_QUEUE_IN_OUT_HPP
2 #define OPENPOSE_THREAD_THREAD_QUEUE_IN_OUT_HPP
3 
8 
9 namespace op
10 {
11  template<typename TDatums, typename TWorker = std::shared_ptr<Worker<TDatums>>, typename TQueue = Queue<TDatums>>
12  class SubThreadQueueInOut : public SubThread<TDatums, TWorker>
13  {
14  public:
15  SubThreadQueueInOut(const std::vector<TWorker>& tWorkers, const std::shared_ptr<TQueue>& tQueueIn, const std::shared_ptr<TQueue>& tQueueOut);
16 
17  bool work();
18 
19  private:
20  std::shared_ptr<TQueue> spTQueueIn;
21  std::shared_ptr<TQueue> spTQueueOut;
22 
23  DELETE_COPY(SubThreadQueueInOut);
24  };
25 }
26 
27 
28 
29 
30 
31 // Implementation
32 namespace op
33 {
34  template<typename TDatums, typename TWorker, typename TQueue>
35  SubThreadQueueInOut<TDatums, TWorker, TQueue>::SubThreadQueueInOut(const std::vector<TWorker>& tWorkers, const std::shared_ptr<TQueue>& tQueueIn,
36  const std::shared_ptr<TQueue>& tQueueOut) :
37  SubThread<TDatums, TWorker>{tWorkers},
38  spTQueueIn{tQueueIn},
39  spTQueueOut{tQueueOut}
40  {
41  // spTQueueIn->addPopper();
42  spTQueueOut->addPusher();
43  }
44 
45  template<typename TDatums, typename TWorker, typename TQueue>
47  {
48  try
49  {
50  // If output queue is closed -> close input queue
51  if (!spTQueueOut->isRunning())
52  {
53  spTQueueIn->stop();
54  return false;
55  }
56  // If output queue running -> normal operation
57  else
58  {
59  // Pop TDatums
60  TDatums tDatums;
61  bool workersAreRunning = spTQueueIn->tryPop(tDatums);
62  // Check queue not stopped
63  if (!workersAreRunning)
64  workersAreRunning = spTQueueIn->isRunning();
65  // Process TDatums
66  workersAreRunning = this->workTWorkers(tDatums, workersAreRunning);
67  // Push/emplace tDatums if successfully processed
68  if (workersAreRunning)
69  {
70  if (tDatums != nullptr)
71  spTQueueOut->waitAndEmplace(tDatums);
72  }
73  // Close both queues otherwise
74  else
75  {
76  spTQueueIn->stop();
77  spTQueueOut->stopPusher();
78  }
79  return workersAreRunning;
80  }
81  }
82  catch (const std::exception& e)
83  {
84  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
85  spTQueueIn->stop();
86  spTQueueOut->stop();
87  return false;
88  }
89  }
90 
91  COMPILE_TEMPLATE_DATUM(SubThreadQueueInOut);
92 }
93 
94 #endif // OPENPOSE_THREAD_THREAD_QUEUE_IN_OUT_HPP
Definition: subThread.hpp:10
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
SubThreadQueueInOut(const std::vector< TWorker > &tWorkers, const std::shared_ptr< TQueue > &tQueueIn, const std::shared_ptr< TQueue > &tQueueOut)
Definition: subThreadQueueInOut.hpp:35
spTQueueIn
Definition: subThreadQueueIn.hpp:36
spTQueueOut
Definition: subThreadQueueInOut.hpp:39
Definition: subThreadQueueInOut.hpp:12
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)