RingQueue.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #ifndef RINGQUEUE_H
  2. #define RINGQUEUE_H
  3. #include <cstdlib>
  4. #include <utility>
  5. #include <mutex>
  6. #include <condition_variable>
  7. // #include <atomic>
  8. /**
  9. * @brief 这里采用零公摊的方式,设置多大的空间,就有多大的空间可以使用
  10. * 1、如果队列是空,m_rear和m_front都设置为-1
  11. * 2、m_rear指向最新入队的元素的下一个位置
  12. * 3、m_front指向需要出队的第一个元素
  13. * 4、环形队列自带互斥锁
  14. *
  15. * 判断队列满:
  16. * m_rear == m_front,并且此时都不等于 -1
  17. *
  18. * 判断队列空:
  19. * m_rear == m_front,并且都等于 -1
  20. *
  21. * 获取队列大小:
  22. * 基本原则就是m_rear后面跟着的是有效值,m_front后面跟着的是已经出队的大小
  23. * m_rear > m_front,返回 m_rear - m_front
  24. * m_front > m_rear,返回 m_capacity - (m_front - m_rear)
  25. * m_rear == m_front,且不等于-1,返回 m_capacity
  26. * m_rear == m_front,且等于-1,返回 0
  27. *
  28. * @tparam T 模版类型
  29. */
  30. template<typename T>
  31. class RingQueue
  32. {
  33. public:
  34. RingQueue();
  35. RingQueue(long size);
  36. ~RingQueue();
  37. /* 设置队列大小 */
  38. void setQueueSize(long size);
  39. long QueueSize() { return m_capacity; }
  40. /* 清空队列 */
  41. void clearQueue();
  42. /* 入队 */
  43. bool enQueue(const T& data);
  44. bool enQueue(T&& data);
  45. /* 阻塞入队 */
  46. void enQueueBlock(const T& data);
  47. /* 出队 */
  48. bool deQueue(T& data);
  49. /* 阻塞出队 */
  50. T& deQueueBlock();
  51. /* 获取队列中第一个值(下一个出队的元素),但是不出队,非阻塞 */
  52. bool front(T& t);
  53. T& frontBlock();
  54. /* 获取队列中即将入队的位置(这个函数用于存储指针的环形队列,获取内存地址) */
  55. T& backBlock();
  56. /* 获取队列大小,队列中有效值的大小 */
  57. long getQueueSize();
  58. /* 获取队列容量 */
  59. long getQueueCapacity();
  60. /* 判断队列是否为空 */
  61. bool isEmpty();
  62. /* 判断队列是否已满 */
  63. bool isFull();
  64. private:
  65. std::mutex m_mutex; /* 互斥锁 */
  66. T* m_queue = nullptr; /* 队列 */
  67. long m_capacity = 0; /* 队列容量 */
  68. long m_front = 0; /* 队头 */
  69. long m_rear = 0; /* 队尾 */
  70. std::condition_variable m_cond_NoFull; /* 非空条件变量 */
  71. std::condition_variable m_cond_NoEmpty; /* 非满条件变量 */
  72. };
  73. /* =====================================================================
  74. * ***************************** 函数实现 *****************************
  75. * ===================================================================== */
  76. /* 这个构造函数需要调用 setQueueSize 设置环形队列的大小 */
  77. template<typename T>
  78. RingQueue<T>::RingQueue() : m_capacity(0) , m_front(-1), m_rear(-1)
  79. {
  80. }
  81. template<typename T>
  82. RingQueue<T>::RingQueue(long capacicy) : m_capacity(capacicy)
  83. {
  84. m_front = -1;
  85. m_rear = -1;
  86. m_queue = new T[m_capacity];
  87. }
  88. template<typename T>
  89. RingQueue<T>::~RingQueue()
  90. {
  91. if(m_queue != nullptr)
  92. {
  93. delete[] m_queue;
  94. m_queue = nullptr;
  95. }
  96. }
  97. /**
  98. * @brief 设置队列大小
  99. * 注意:使用这个设置,如果队列中存储的是指针,指针的内存区域需要在调用这个函数之前释放,不然可能会造成
  100. * 内存泄漏
  101. *
  102. * @tparam T
  103. * @param size
  104. */
  105. template<typename T>
  106. void RingQueue<T>::setQueueSize(long size)
  107. {
  108. if(m_queue != nullptr)
  109. {
  110. delete[] m_queue;
  111. m_queue = nullptr;
  112. }
  113. m_capacity = size;
  114. m_front = -1;
  115. m_rear = -1;
  116. m_queue = new T[m_capacity];
  117. }
  118. /* 清空队列 */
  119. template<typename T>
  120. void RingQueue<T>::clearQueue()
  121. {
  122. m_mutex.lock();
  123. if(m_queue != nullptr)
  124. {
  125. delete[] m_queue;
  126. m_queue = nullptr;
  127. }
  128. m_front = -1;
  129. m_rear = -1;
  130. m_mutex.unlock();
  131. }
  132. /* 入队 */
  133. template<typename T>
  134. bool RingQueue<T>::enQueue(const T& data)
  135. {
  136. m_mutex.lock();
  137. /* 先检查队列是否还有剩余空间 */
  138. if(isFull())
  139. {
  140. return false;
  141. }
  142. else if(m_rear == -1)
  143. {
  144. m_front = 0;
  145. m_rear = 0;
  146. }
  147. /* 数据入队 */
  148. m_queue[m_rear] = data;
  149. m_rear = (m_rear + 1) % m_capacity;
  150. m_mutex.unlock();
  151. m_cond_NoEmpty.notify_all();
  152. return true;
  153. }
  154. /* 入队,传入右值 */
  155. template<typename T>
  156. bool RingQueue<T>::enQueue(T&& data)
  157. {
  158. m_mutex.lock();
  159. /* 先检查队列是否还有剩余空间 */
  160. if(isFull())
  161. {
  162. return false;
  163. }
  164. else if(m_rear == -1)
  165. {
  166. m_front = 0;
  167. m_rear = 0;
  168. }
  169. m_queue[m_rear] = std::move(data);
  170. m_rear = (m_rear + 1) % m_capacity;
  171. m_mutex.unlock();
  172. m_cond_NoEmpty.notify_all();
  173. return true;
  174. }
  175. /* 阻塞入队 */
  176. template<typename T>
  177. void RingQueue<T>::enQueueBlock(const T& data)
  178. {
  179. std::unique_lock<std::mutex> lock(m_mutex);
  180. m_cond_NoFull.wait(lock, [this](){
  181. return !isFull();
  182. });
  183. if(m_rear == -1)
  184. {
  185. m_front = 0;
  186. m_rear = 0;
  187. }
  188. m_queue[m_rear] = data;
  189. m_rear = (m_rear + 1) % m_capacity;
  190. m_mutex.unlock();
  191. m_cond_NoEmpty.notify_all();
  192. }
  193. /* 出队 */
  194. template<typename T>
  195. bool RingQueue<T>::deQueue(T& data)
  196. {
  197. m_mutex.lock();
  198. if(isEmpty())
  199. {
  200. return false;
  201. }
  202. data = std::move(m_queue[m_front]);
  203. /* 判断队列是否为空了 */
  204. m_front = (m_front + 1) % m_capacity;
  205. if(m_front == m_rear)
  206. {
  207. m_front = -1;
  208. m_rear = -1;
  209. }
  210. m_mutex.unlock();
  211. m_cond_NoFull.notify_all();
  212. return true;
  213. }
  214. /* 出队 */
  215. template<typename T>
  216. T& RingQueue<T>::deQueueBlock()
  217. {
  218. std::unique_lock<std::mutex> lock(m_mutex);
  219. m_cond_NoEmpty.wait(lock, [this](){
  220. return !isEmpty();
  221. });
  222. long tmp = m_front;
  223. m_front = (m_front + 1) % m_capacity;
  224. if(m_front == m_rear)
  225. {
  226. m_front = -1;
  227. m_rear = -1;
  228. }
  229. m_cond_NoFull.notify_all();
  230. return m_queue[tmp];
  231. }
  232. /* 获取队列中第一个值(下一个出队的元素),但是不出队,非阻塞 */
  233. template<typename T>
  234. bool RingQueue<T>::front(T& t)
  235. {
  236. std::lock_guard<std::mutex> lock(m_mutex);
  237. if(isEmpty())
  238. {
  239. return false;
  240. }
  241. t = m_queue[m_front];
  242. return true;
  243. }
  244. /* 阻塞获取队列中第一个数据,但是不出队 */
  245. template<typename T>
  246. T& RingQueue<T>::frontBlock()
  247. {
  248. std::lock_guard<std::mutex> lock(m_mutex);
  249. m_cond_NoEmpty.wait(lock, [this](){
  250. return !isEmpty();
  251. });
  252. return m_queue[m_front];
  253. }
  254. /**
  255. * @brief 获取队列中即将入队的位置(这个函数用于存储指针的环形队列,获取内存地址)
  256. * 这个函数不一定内存安全,谨慎使用
  257. *
  258. * @tparam T
  259. * @return T&
  260. */
  261. template<typename T>
  262. T& RingQueue<T>::backBlock()
  263. {
  264. std::lock_guard<std::mutex> lock(m_mutex);
  265. m_cond_NoFull.wait(lock, [this](){
  266. return !isFull();
  267. });
  268. return m_queue[m_rear];
  269. }
  270. /* 获取队列中有效值的大小 */
  271. template<typename T>
  272. long RingQueue<T>::getQueueSize()
  273. {
  274. if(m_rear == -1)
  275. {
  276. return 0;
  277. }
  278. else if(m_rear > m_front)
  279. {
  280. return m_rear - m_front;
  281. }
  282. else if(m_rear < m_front)
  283. {
  284. return m_capacity - ( m_front - m_rear );
  285. }
  286. /* 这时候是队列满 */
  287. return m_capacity;
  288. }
  289. /* 获取队列容量 */
  290. template<typename T>
  291. long RingQueue<T>::getQueueCapacity()
  292. {
  293. return m_capacity;
  294. }
  295. /**
  296. * @brief 判断队列是否为空
  297. *
  298. * @tparam T
  299. * @return true
  300. * @return false
  301. */
  302. template<typename T>
  303. bool RingQueue<T>::isEmpty()
  304. {
  305. if((m_front == m_rear) && (m_front == -1))
  306. {
  307. return true;
  308. }
  309. return false;
  310. }
  311. /**
  312. * @brief 判断队列是否已满,这里判断依赖入队和出队后的队头和队尾指针的位置
  313. * 1、队头和队尾指针相等,但是队尾指针不等于-1,表示队列已满
  314. *
  315. * @tparam T
  316. * @return true
  317. * @return false
  318. */
  319. template<typename T>
  320. bool RingQueue<T>::isFull()
  321. {
  322. /* 如果m_rear或者m_front不等于-1,说明此时里面有内容
  323. * 同时m_front == m_rear,队列就满了 */
  324. if(m_front == m_rear && m_rear != -1)
  325. {
  326. return true;
  327. }
  328. return false;
  329. }
  330. #endif /* RINGQUEUE_H */