13 #ifndef HTGS_BLOCKINGQUEUE_HPP 14 #define HTGS_BLOCKINGQUEUE_HPP 18 #include <condition_variable> 47 queueActiveMaxSize = 0;
64 queueActiveMaxSize = 0;
81 std::cerr << __FILE__ <<
":" << __LINE__
82 <<
"ERROR: Requesting remaining capacity on BlockingQueue that does not have a max size" << std::endl;
102 std::unique_lock<std::mutex> lock(this->
mutex);
115 T res = this->
queue.front();
129 auto start = std::chrono::high_resolution_clock::now();
131 std::unique_lock<std::mutex> lock(this->
mutex);
133 auto end = std::chrono::high_resolution_clock::now();
134 this->enqueueLockTime += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
138 start = std::chrono::high_resolution_clock::now();
142 end = std::chrono::high_resolution_clock::now();
143 this->enqueueWaitTime += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
149 if (
queue.size() > queueActiveMaxSize)
150 queueActiveMaxSize =
queue.size();
164 auto start = std::chrono::high_resolution_clock::now();
166 std::unique_lock<std::mutex> lock(this->
mutex);
168 auto end = std::chrono::high_resolution_clock::now();
169 this->dequeueLockTime += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
170 start = std::chrono::high_resolution_clock::now();
174 end = std::chrono::high_resolution_clock::now();
175 this->dequeueWaitTime += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
177 T res = this->
queue.front();
190 std::unique_lock<std::mutex> lock(this->
mutex);
191 if (this->
condition.wait_for(lock, std::chrono::microseconds(timeout),
192 [=] { return !this->queue.empty(); })) {
193 T res = this->
queue.front();
203 unsigned long long int getEnqueueLockTime()
const {
204 return enqueueLockTime;
206 unsigned long long int getDequeueLockTime()
const {
207 return dequeueLockTime;
209 unsigned long long int getEnqueueWaitTime()
const {
210 return enqueueWaitTime;
212 unsigned long long int getDequeueWaitTime()
const {
213 return dequeueWaitTime;
218 size_t getQueueActiveMaxSize()
const {
219 return queueActiveMaxSize;
222 void resetMaxQueueSize() {
223 queueActiveMaxSize = 0;
229 unsigned long long int enqueueLockTime;
230 unsigned long long int dequeueLockTime;
231 unsigned long long int enqueueWaitTime;
232 unsigned long long int dequeueWaitTime;
236 size_t queueActiveMaxSize;
245 #endif //HTGS_BLOCKINGQUEUE_HPP ~BlockingQueue()
Destructor.
Definition: BlockingQueue.hpp:71
bool isEmpty()
Gets whether the queue is empty or not.
Definition: BlockingQueue.hpp:93
size_t size()
Gets the number of elements in the queue.
Definition: BlockingQueue.hpp:101
Creates a thread-safe queue that will wait when no data is available and can block if the queue is fu...
Definition: BlockingQueue.hpp:32
std::mutex mutex
The mutex to ensure thread safety.
Definition: BlockingQueue.hpp:240
void Enqueue(T const &value)
Adds an element into the queue.
Definition: BlockingQueue.hpp:126
std::condition_variable condition
The condition variable used for waking up waiting threads.
Definition: BlockingQueue.hpp:241
T Dequeue()
Removes an element from the queue.
Definition: BlockingQueue.hpp:162
T poll(size_t timeout)
Polls for data given the specified timeout time in microseconds.
Definition: BlockingQueue.hpp:189
size_t remainingCapacity()
Gets the remaining capacity of the queue based on the queueSize.
Definition: BlockingQueue.hpp:79
BlockingQueue(size_t qSize)
Creates a blocking queue that will block a data requester when the queue is empty or full...
Definition: BlockingQueue.hpp:55
size_t queueSize
The maximum size of the queue, set to -1 for infinite size.
Definition: BlockingQueue.hpp:238
Definition: Bookkeeper.hpp:23
BlockingQueue()
Creates a blocking queue that will only block a data requester when the queue is empty.
Definition: BlockingQueue.hpp:37
std::queue< T > queue
The FIFO queue.
Definition: BlockingQueue.hpp:239