RingQueue.hpp 7.8 KB

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