|
@@ -39,6 +39,7 @@ class RingQueue
|
|
|
public:
|
|
|
RingQueue();
|
|
|
RingQueue(long size);
|
|
|
+ RingQueue(long size, T defaultValue);
|
|
|
~RingQueue();
|
|
|
|
|
|
/* 入队,默认是阻塞入队 */
|
|
@@ -56,15 +57,21 @@ public:
|
|
|
T front();
|
|
|
/* 非阻塞的方式获取,队列为空返回false */
|
|
|
bool front_NoBlock(T& t);
|
|
|
+ /* 非阻塞方式获取第一个值,如果对列为空,则会返回设置的默认值 */
|
|
|
+ T front_NoBlock();
|
|
|
|
|
|
/* 获取对立第一个数据,获取完立刻出队
|
|
|
* 如果队列为空,会阻塞住,直到有数据为止 */
|
|
|
T&& front_pop();
|
|
|
- // T&& front_pop_rvalue();
|
|
|
+ /* 非阻塞方式获取第一个值,并出队 */
|
|
|
bool front_pop_NoBlock(T& t);
|
|
|
+ /* 非阻塞方式获取第一个值,并出队,如果队列为空,会返回设置的默认值 */
|
|
|
+ T&& front_pop_NoBlock();
|
|
|
|
|
|
/* 设置队列大小 */
|
|
|
void setQueueCapacity(long size);
|
|
|
+ /* 设置默认值,给指针类型使用,如果是非阻塞获取,空的时候可以返回为设置的默认值(如nullptr) */
|
|
|
+ void setDefaultValue(T defaultValue);
|
|
|
/* 获取队列大小,队列中有效值的大小 */
|
|
|
long QueueSize();
|
|
|
/* 获取队列容量 */
|
|
@@ -81,10 +88,11 @@ public:
|
|
|
private:
|
|
|
bool m_isExit = false; /* 是否退出,这个标识位是为了退出阻塞住的函数 */
|
|
|
std::mutex m_mutex; /* 互斥锁 */
|
|
|
+ T m_defaultValue; /* 默认值 */
|
|
|
T* m_queue = nullptr; /* 队列 */
|
|
|
- long m_capacity = 0; /* 队列容量 */
|
|
|
- long m_front = 0; /* 队头 */
|
|
|
- long m_rear = 0; /* 队尾 */
|
|
|
+ long m_capacity = 0; /* 队列容量 */
|
|
|
+ long m_front = 0; /* 队头 */
|
|
|
+ long m_rear = 0; /* 队尾 */
|
|
|
std::condition_variable m_cond_NoFull; /* 非满条件变量 */
|
|
|
std::condition_variable m_cond_NoEmpty; /* 非空条件变量 */
|
|
|
};
|
|
@@ -109,6 +117,20 @@ RingQueue<T>::RingQueue(long capacicy) : m_capacity(capacicy)
|
|
|
m_queue = new T[m_capacity];
|
|
|
}
|
|
|
|
|
|
+/* 添加默认值 */
|
|
|
+template<typename T>
|
|
|
+RingQueue<T>::RingQueue(long size, T defaultValue)
|
|
|
+{
|
|
|
+ m_front = -1;
|
|
|
+ m_rear = -1;
|
|
|
+ m_queue = new T[m_capacity];
|
|
|
+ for(long i = 0; i < m_capacity; i++)
|
|
|
+ {
|
|
|
+ m_queue[i] = defaultValue;
|
|
|
+ }
|
|
|
+ m_defaultValue = defaultValue;
|
|
|
+}
|
|
|
+
|
|
|
template<typename T>
|
|
|
RingQueue<T>::~RingQueue()
|
|
|
{
|
|
@@ -311,12 +333,29 @@ bool RingQueue<T>::front_NoBlock(T& t)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/* 非阻塞方式获取第一个值,如果对列为空,则会返回设置的默认值 */
|
|
|
+template<typename T>
|
|
|
+T RingQueue<T>::front_NoBlock()
|
|
|
+{
|
|
|
+ T ret;
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
+ if(isEmpty())
|
|
|
+ {
|
|
|
+ return m_defaultValue;
|
|
|
+ }
|
|
|
+ ret = m_queue[m_front];
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
/* 获取对立第一个数据,获取完立刻出队
|
|
|
* 如果队列为空,会阻塞住,直到有数据为止 */
|
|
|
template<typename T>
|
|
|
T&& RingQueue<T>::front_pop()
|
|
|
{
|
|
|
- T ret;
|
|
|
+ T ret = m_defaultValue;
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
m_cond_NoEmpty.wait(lock, [this](){
|
|
@@ -338,7 +377,7 @@ T&& RingQueue<T>::front_pop()
|
|
|
return std::move(ret);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+/* 非阻塞方式获取第一个值,并出队 */
|
|
|
template<typename T>
|
|
|
bool RingQueue<T>::front_pop_NoBlock(T& t)
|
|
|
{
|
|
@@ -360,6 +399,32 @@ bool RingQueue<T>::front_pop_NoBlock(T& t)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+/* 非阻塞方式获取第一个值,并出队 */
|
|
|
+template<typename T>
|
|
|
+T&& RingQueue<T>::front_pop_NoBlock()
|
|
|
+{
|
|
|
+ T ret = m_defaultValue;
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
+ m_cond_NoEmpty.wait(lock, [this](){
|
|
|
+ return (!isEmpty() || m_isExit);
|
|
|
+ });
|
|
|
+ if(m_isExit)
|
|
|
+ {
|
|
|
+ return std::move(ret);
|
|
|
+ }
|
|
|
+ ret = std::move(m_queue[m_front]);
|
|
|
+ m_front = (m_front + 1) % m_capacity;
|
|
|
+ if(m_front == m_rear)
|
|
|
+ {
|
|
|
+ m_front = -1;
|
|
|
+ m_rear = -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ m_cond_NoFull.notify_all();
|
|
|
+ return std::move(ret);
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @brief 设置队列大小
|
|
@@ -384,6 +449,14 @@ void RingQueue<T>::setQueueCapacity(long size)
|
|
|
m_queue = new T[m_capacity];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+/* 设置默认值,给指针类型使用,如果是非阻塞获取,空的时候可以返回为设置的默认值(如nullptr) */
|
|
|
+template<typename T>
|
|
|
+void RingQueue<T>::setDefaultValue(T defaultValue)
|
|
|
+{
|
|
|
+ m_defaultValue = defaultValue;
|
|
|
+}
|
|
|
+
|
|
|
/* 获取队列中有效值的大小 */
|
|
|
template<typename T>
|
|
|
long RingQueue<T>::QueueSize()
|