123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- #ifndef RINGQUEUE_H
- #define RINGQUEUE_H
- #include <cstdlib>
- #include <utility>
- #include <mutex>
- #include <condition_variable>
- template<typename T>
- class RingQueue
- {
- public:
- RingQueue();
- RingQueue(long size);
- ~RingQueue();
-
- void push(const T& value);
- void push(T&& value);
- bool push_NoBlock(const T& value);
- bool push_NoBlock(T&& value);
-
- void pop();
-
- T front();
-
- bool front_NoBlock(T& t);
-
- T&& front_pop();
-
- bool front_pop_NoBlock(T& t);
-
-
- void setQueueCapacity(long size);
-
- long QueueSize();
-
- long QueueCapacity();
-
- bool isEmpty();
-
- bool isFull();
-
- void clearQueue();
-
- void exit();
- private:
- bool m_isExit = false;
- std::mutex m_mutex;
- T* m_queue = nullptr;
- long m_capacity = 0;
- long m_front = 0;
- long m_rear = 0;
- std::condition_variable m_cond_NoFull;
- std::condition_variable m_cond_NoEmpty;
- };
- template<typename T>
- RingQueue<T>::RingQueue() : m_capacity(0) , m_front(-1), m_rear(-1)
- {
-
- }
- template<typename T>
- RingQueue<T>::RingQueue(long capacicy) : m_capacity(capacicy)
- {
- m_front = -1;
- m_rear = -1;
- m_queue = new T[m_capacity];
- }
- template<typename T>
- RingQueue<T>::~RingQueue()
- {
- if(m_queue != nullptr)
- {
- delete[] m_queue;
- m_queue = nullptr;
- }
- }
- template<typename T>
- void RingQueue<T>::clearQueue()
- {
- m_mutex.lock();
- if(m_queue != nullptr)
- {
- delete[] m_queue;
- m_queue = nullptr;
- }
- m_front = -1;
- m_rear = -1;
- m_mutex.unlock();
- }
- template<typename T>
- void RingQueue<T>::push(const T& value)
- {
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_cond_NoFull.wait(lock, [this](){
- return (!isFull() || m_isExit);
- });
- if(m_isExit)
- {
- return;
- }
- if(m_rear == -1)
- {
- m_front = 0;
- m_rear = 0;
- }
- m_queue[m_rear] = value;
- m_rear = (m_rear + 1) % m_capacity;
- }
- m_cond_NoEmpty.notify_all();
- }
- template<typename T>
- void RingQueue<T>::push(T&& value)
- {
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_cond_NoFull.wait(lock, [this](){
- return (!isFull() || m_isExit);
- });
- if(m_isExit)
- {
- return;
- }
- if(m_rear == -1)
- {
- m_front = 0;
- m_rear = 0;
- }
- m_queue[m_rear] = std::move(value);
- m_rear = (m_rear + 1) % m_capacity;
- }
- m_cond_NoEmpty.notify_all();
- }
- template<typename T>
- bool RingQueue<T>::push_NoBlock(const T& value)
- {
- {
-
-
-
-
-
- std::lock_guard<std::mutex> lock(m_mutex);
-
- if(isFull())
- {
- return false;
- }
- else if(m_rear == -1)
- {
- m_front = 0;
- m_rear = 0;
- }
- m_queue[m_rear] = value;
- m_rear = (m_rear + 1) % m_capacity;
- }
- m_cond_NoEmpty.notify_all();
- return true;
- }
- template<typename T>
- bool RingQueue<T>::push_NoBlock(T&& value)
- {
- {
-
-
-
-
-
- std::lock_guard<std::mutex> lock(m_mutex);
-
- if(isFull())
- {
- return false;
- }
- else if(m_rear == -1)
- {
- m_front = 0;
- m_rear = 0;
- }
- m_queue[m_rear] = std::move(value);
- m_rear = (m_rear + 1) % m_capacity;
- }
- m_cond_NoEmpty.notify_all();
- return true;
- }
- template<typename T>
- void RingQueue<T>::pop()
- {
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- if(isEmpty())
- {
- return;
- }
- m_front = (m_front + 1) % m_capacity;
- if(m_front == m_rear)
- {
- m_front = -1;
- m_rear = -1;
- }
- }
- m_cond_NoFull.notify_all();
- }
- template<typename T>
- T RingQueue<T>::front()
- {
- T retValue;
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_cond_NoEmpty.wait(lock, [this](){
- return (!isEmpty() || m_isExit);
- });
- if(m_isExit)
- {
- return retValue;
- }
- retValue = m_queue[m_front];
- }
- return retValue;
- }
- template<typename T>
- bool RingQueue<T>::front_NoBlock(T& t)
- {
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- if(isEmpty())
- {
- return false;
- }
- t = m_queue[m_front];
- }
- return true;
- }
- template<typename T>
- T&& RingQueue<T>::front_pop()
- {
- T ret;
- {
- 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);
- }
- template<typename T>
- bool RingQueue<T>::front_pop_NoBlock(T& t)
- {
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- if(isEmpty())
- {
- return false;
- }
- t = 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 true;
- }
- template<typename T>
- void RingQueue<T>::setQueueCapacity(long size)
- {
- if(m_queue != nullptr)
- {
- delete[] m_queue;
- m_queue = nullptr;
- }
- m_capacity = size;
- m_front = -1;
- m_rear = -1;
- m_queue = new T[m_capacity];
- }
- template<typename T>
- long RingQueue<T>::QueueSize()
- {
- std::lock_guard<std::mutex> lock(m_mutex);
- if(m_rear == -1)
- {
- return 0;
- }
- else if(m_rear > m_front)
- {
- return m_rear - m_front;
- }
- else if(m_rear < m_front)
- {
- return m_capacity - ( m_front - m_rear );
- }
-
- return m_capacity;
- }
- template<typename T>
- long RingQueue<T>::QueueCapacity()
- {
- std::lock_guard<std::mutex> lock(m_mutex);
- return m_capacity;
- }
- template<typename T>
- bool RingQueue<T>::isEmpty()
- {
- if((m_front == m_rear) && (m_front == -1))
- {
- return true;
- }
- return false;
- }
- template<typename T>
- bool RingQueue<T>::isFull()
- {
-
- if(m_front == m_rear && m_rear != -1)
- {
- return true;
- }
-
- return false;
- }
- template<typename T>
- void RingQueue<T>::exit()
- {
- m_isExit = true;
- m_cond_NoFull.notify_all();
- m_cond_NoEmpty.notify_all();
- }
- #endif
|