kNOw SIGN
ThreadSafeFramesQueue.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #include <limits>
9 #include <queue>
10 #include <mutex>
11 #include <thread>
13 #include <opencv2/core/utility.hpp>
15 
23 template <typename T>
24 class ThreadSafeFramesQueue : public std::queue<T> {
25 public:
30  : maxDepth(5) {
31  tm.reset();
32  tm.start();
33  }
34 
38  explicit ThreadSafeFramesQueue(size_t const& _maxDepth)
39  : maxDepth(_maxDepth) {
40  tm.reset();
41  tm.start();
42  }
43 
47  void Push(const T& entry) noexcept {
48  std::lock_guard<std::mutex> lock(mtx);
50  while (this->size() >= maxDepth) {
51  T elem;
52  static_cast<void>(Pop(elem));
53  }
54  this->push(entry);
55  }
56 
60  bool Pop(T& out) noexcept {
61  std::lock_guard<std::mutex> lock(mtx);
63  if (this->empty()) {
64  return false;
65  }
67  out = this->front();
68  this->pop();
70  tm.stop();
72  tm.start();
74  return true;
75  }
76 
81  double GetFPS() noexcept {
82  std::lock_guard<std::mutex> lock(mtx);
83  return tm.getFPS();
84  }
85 
89  void Clear() noexcept {
90  std::lock_guard<std::mutex> lock(mtx);
91  while (!this->empty()) {
92  this->pop();
93  }
94  }
95 
96 private:
97  cv::TickMeter tm;
98  std::mutex mtx;
99  size_t const maxDepth;
100 };
size_t const maxDepth
mutex for safe read/write access of/to queue internals
Definition: ThreadSafeFramesQueue.hpp:99
ThreadSafeFramesQueue(size_t const &_maxDepth)
Definition: ThreadSafeFramesQueue.hpp:38
std::mutex mtx
OpenCV timer class.
Definition: ThreadSafeFramesQueue.hpp:98
cv::TickMeter tm
Definition: ThreadSafeFramesQueue.hpp:97
thread-safe queue (FIFO) implementation (with timer)
Definition: ThreadSafeFramesQueue.hpp:24
double GetFPS() noexcept
Definition: ThreadSafeFramesQueue.hpp:81
void Clear() noexcept
Definition: ThreadSafeFramesQueue.hpp:89
ThreadSafeFramesQueue()
Definition: ThreadSafeFramesQueue.hpp:29
void Push(const T &entry) noexcept
Definition: ThreadSafeFramesQueue.hpp:47
bool Pop(T &out) noexcept
Definition: ThreadSafeFramesQueue.hpp:60