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
subThreadQueueOut.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_THREAD_THREAD_QUEUE_OUT_HPP
2 #define OPENPOSE_THREAD_THREAD_QUEUE_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 SubThreadQueueOut : public SubThread<TDatums, TWorker>
13  {
14  public:
15  SubThreadQueueOut(const std::vector<TWorker>& tWorkers, const std::shared_ptr<TQueue>& tQueueOut);
16 
17  bool work();
18 
19  private:
20  std::shared_ptr<TQueue> spTQueueOut;
21 
22  DELETE_COPY(SubThreadQueueOut);
23  };
24 }
25 
26 
27 
28 
29 
30 // Implementation
31 namespace op
32 {
33  template<typename TDatums, typename TWorker, typename TQueue>
35  const std::shared_ptr<TQueue>& tQueueOut) :
36  SubThread<TDatums, TWorker>{tWorkers},
37  spTQueueOut{tQueueOut}
38  {
39  spTQueueOut->addPusher();
40  }
41 
42  template<typename TDatums, typename TWorker, typename TQueue>
44  {
45  try
46  {
47  // If output queue is closed -> close input queue
48  if (!spTQueueOut->isRunning())
49  return false;
50  else
51  {
52  // Don't work until next queue is not full
53  // This reduces latency to half
54  if (!spTQueueOut->isFull())
55  {
56  // Process TDatums
57  TDatums tDatums;
58  const auto workersAreRunning = this->workTWorkers(tDatums, true);
59  // Push/emplace tDatums if successfully processed
60  if (workersAreRunning)
61  {
62  if (tDatums != nullptr)
63  spTQueueOut->waitAndEmplace(tDatums);
64  }
65  // Close queue otherwise
66  else
67  spTQueueOut->stopPusher();
68  return workersAreRunning;
69  }
70  else
71  {
72  std::this_thread::sleep_for(std::chrono::microseconds{100});
73  return true;
74  }
75  }
76  }
77  catch (const std::exception& e)
78  {
79  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
80  spTQueueOut->stop();
81  return false;
82  }
83  }
84 
85  COMPILE_TEMPLATE_DATUM(SubThreadQueueOut);
86 }
87 
88 #endif // OPENPOSE_THREAD_THREAD_QUEUE_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="")
spTQueueOut
Definition: subThreadQueueInOut.hpp:39
Definition: subThreadQueueOut.hpp:12
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)
SubThreadQueueOut(const std::vector< TWorker > &tWorkers, const std::shared_ptr< TQueue > &tQueueOut)
Definition: subThreadQueueOut.hpp:34