rename PIBlockingDequeue -> PIBlockingQueue

This commit is contained in:
2020-08-11 19:05:47 +03:00
parent 8c3349d84a
commit 3ba6a7b0e8
3 changed files with 43 additions and 38 deletions

View File

@@ -17,8 +17,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIBLOCKINGDEQUEUE_H
#define PIBLOCKINGDEQUEUE_H
#ifndef PIBLOCKINGQUEUE_H
#define PIBLOCKINGQUEUE_H
#include "pideque.h"
#include "piconditionvar.h"
@@ -28,13 +28,13 @@
* wait for space to become available in the queue when storing an element.
*/
template <typename T>
class PIBlockingDequeue: private PIDeque<T> {
class PIBlockingQueue: private PIQueue<T> {
public:
/**
* @brief Constructor
*/
explicit inline PIBlockingDequeue(size_t capacity = SIZE_MAX,
explicit inline PIBlockingQueue(size_t capacity = SIZE_MAX,
PIConditionVariable* cond_var_add = new PIConditionVariable(),
PIConditionVariable* cond_var_rem = new PIConditionVariable())
: cond_var_add(cond_var_add), cond_var_rem(cond_var_rem), max_size(capacity) { }
@@ -42,7 +42,7 @@ public:
/**
* @brief Copy constructor. Initialize queue with copy of other queue elements. Not thread-safe for other queue.
*/
explicit inline PIBlockingDequeue(const PIDeque<T>& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
explicit inline PIBlockingQueue(const PIDeque<T>& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
mutex.lock();
max_size = SIZE_MAX;
PIDeque<T>::append(other);
@@ -52,7 +52,7 @@ public:
/**
* @brief Thread-safe copy constructor. Initialize queue with copy of other queue elements.
*/
inline PIBlockingDequeue(PIBlockingDequeue<T> & other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
inline PIBlockingQueue(PIBlockingQueue<T> & other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
other.mutex.lock();
mutex.lock();
max_size = other.max_size;
@@ -61,7 +61,7 @@ public:
other.mutex.unlock();
}
~PIBlockingDequeue() {
~PIBlockingQueue() {
delete cond_var_add;
delete cond_var_rem;
}
@@ -71,14 +71,17 @@ public:
*
* @param v the element to add
*/
void put(const T & v) {
PIBlockingQueue<T> & put(const T & v) {
mutex.lock();
cond_var_rem->wait(mutex, [&]() { return PIDeque<T>::size() < max_size; });
PIDeque<T>::push_back(v);
mutex.unlock();
cond_var_add->notifyOne();
return *this;
}
PIBlockingQueue<T> & enqueue(const T & v) {return put(v);}
/**
* @brief Inserts the specified element at the end of this queue if it is possible to do so immediately without
* exceeding the queue's capacity, returning true upon success and false if this queue is full.
@@ -114,6 +117,8 @@ public:
return t;
}
T dequeue() {return take();}
/**
* @brief Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an
* element to become available.
@@ -190,7 +195,7 @@ public:
/**
* @brief Removes all available elements from this queue and adds them to other given queue.
*/
size_t drainTo(PIBlockingDequeue<T>& other, size_t maxCount = SIZE_MAX) {
size_t drainTo(PIBlockingQueue<T>& other, size_t maxCount = SIZE_MAX) {
mutex.lock();
other.mutex.lock();
size_t count = maxCount > PIDeque<T>::size() ? PIDeque<T>::size() : maxCount;
@@ -210,4 +215,4 @@ private:
};
#endif // PIBLOCKINGDEQUEUE_H
#endif // PIBLOCKINGQUEUE_H

View File

@@ -20,7 +20,7 @@
#ifndef PITHREADPOOLEXECUTOR_H
#define PITHREADPOOLEXECUTOR_H
#include "piblockingdequeue.h"
#include "piblockingqueue.h"
#include <atomic>
@@ -54,7 +54,7 @@ public:
private:
std::atomic_bool isShutdown_;
PIBlockingDequeue<std::function<void()> > taskQueue;
PIBlockingQueue<std::function<void()> > taskQueue;
PIVector<PIThread*> threadPool;
bool queue_own;