RingQueue.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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指向最新入队的元素的下一个位置,就是下个将要入队的元素位置
  11. * 2、m_front指向需要出队的第一个元素
  12. * 3、环形队列自带互斥锁
  13. *
  14. * 注意:
  15. * 使用时要注意,不带NoBlock的都是阻塞函数
  16. *
  17. * 判断队列满:
  18. * m_rear == m_front,并且此时都不等于 -1
  19. *
  20. * 判断队列空:
  21. * m_rear == m_front,并且都等于 -1
  22. *
  23. * 获取队列大小:
  24. * 基本原则就是m_rear后面跟着的是有效值,m_front后面跟着的是已经出队的大小
  25. * m_rear > m_front,返回 m_rear - m_front
  26. * m_front > m_rear,返回 m_capacity - (m_front - m_rear)
  27. * m_rear == m_front,且不等于-1,返回 m_capacity
  28. * m_rear == m_front,且等于-1,返回 0
  29. *
  30. * @tparam T 模版类型
  31. */
  32. template<typename T>
  33. class RingQueue
  34. {
  35. public:
  36. RingQueue();
  37. RingQueue(long size);
  38. ~RingQueue();
  39. /* 入队,默认是阻塞入队 */
  40. void push(const T& value);
  41. void push(T&& value);
  42. bool push_NoBlock(const T& value);
  43. bool push_NoBlock(T&& value);
  44. /* 出队,删除队列的首个元素
  45. * 注意,如果存储的是指针,需要手动释放该指针指向的内存区域,不然会造成内存泄漏 */
  46. void pop();
  47. /* 获取队列中第一个值),但是不出队
  48. * 阻塞的方式获取,如果队列为空,会一直阻塞住,直到获取到数据为止 */
  49. T front();
  50. /* 非阻塞的方式获取,队列为空返回false */
  51. bool front_NoBlock(T& t);
  52. /* 获取对立第一个数据,获取完立刻出队
  53. * 如果队列为空,会阻塞住,直到有数据为止 */
  54. T&& front_pop();
  55. // T&& front_pop_rvalue();
  56. bool front_pop_NoBlock(T& t);
  57. /* 设置队列大小 */
  58. void setQueueCapacity(long size);
  59. /* 获取队列大小,队列中有效值的大小 */
  60. long QueueSize();
  61. /* 获取队列容量 */
  62. long QueueCapacity();
  63. /* 判断队列是否为空 */
  64. bool isEmpty();
  65. /* 判断队列是否已满 */
  66. bool isFull();
  67. /* 清空队列 */
  68. void clearQueue();
  69. /* 退出所有可能的阻塞函数 */
  70. void exit();
  71. private:
  72. bool m_isExit = false; /* 是否退出,这个标识位是为了退出阻塞住的函数 */
  73. std::mutex m_mutex; /* 互斥锁 */
  74. T* m_queue = nullptr; /* 队列 */
  75. long m_capacity = 0; /* 队列容量 */
  76. long m_front = 0; /* 队头 */
  77. long m_rear = 0; /* 队尾 */
  78. std::condition_variable m_cond_NoFull; /* 非满条件变量 */
  79. std::condition_variable m_cond_NoEmpty; /* 非空条件变量 */
  80. };
  81. /* =====================================================================
  82. * ***************************** 函数实现 *****************************
  83. * ===================================================================== */
  84. /* 这个构造函数需要调用 setQueueSize 设置环形队列的大小 */
  85. template<typename T>
  86. RingQueue<T>::RingQueue() : m_capacity(0) , m_front(-1), m_rear(-1)
  87. {
  88. }
  89. template<typename T>
  90. RingQueue<T>::RingQueue(long capacicy) : m_capacity(capacicy)
  91. {
  92. m_front = -1;
  93. m_rear = -1;
  94. m_queue = new T[m_capacity];
  95. }
  96. template<typename T>
  97. RingQueue<T>::~RingQueue()
  98. {
  99. if(m_queue != nullptr)
  100. {
  101. delete[] m_queue;
  102. m_queue = nullptr;
  103. }
  104. }
  105. /* 清空队列 */
  106. template<typename T>
  107. void RingQueue<T>::clearQueue()
  108. {
  109. m_mutex.lock();
  110. if(m_queue != nullptr)
  111. {
  112. delete[] m_queue;
  113. m_queue = nullptr;
  114. }
  115. m_front = -1;
  116. m_rear = -1;
  117. m_mutex.unlock();
  118. }
  119. /*************** 入队 *******************/
  120. template<typename T>
  121. void RingQueue<T>::push(const T& value)
  122. {
  123. {
  124. std::unique_lock<std::mutex> lock(m_mutex);
  125. m_cond_NoFull.wait(lock, [this](){
  126. return (!isFull() || m_isExit);
  127. });
  128. if(m_isExit)
  129. {
  130. return;
  131. }
  132. if(m_rear == -1)
  133. {
  134. m_front = 0;
  135. m_rear = 0;
  136. }
  137. m_queue[m_rear] = value;
  138. m_rear = (m_rear + 1) % m_capacity;
  139. }
  140. m_cond_NoEmpty.notify_all();
  141. }
  142. template<typename T>
  143. void RingQueue<T>::push(T&& value)
  144. {
  145. {
  146. std::unique_lock<std::mutex> lock(m_mutex);
  147. m_cond_NoFull.wait(lock, [this](){
  148. return (!isFull() || m_isExit);
  149. });
  150. if(m_isExit)
  151. {
  152. return;
  153. }
  154. if(m_rear == -1)
  155. {
  156. m_front = 0;
  157. m_rear = 0;
  158. }
  159. m_queue[m_rear] = std::move(value);
  160. m_rear = (m_rear + 1) % m_capacity;
  161. }
  162. m_cond_NoEmpty.notify_all();
  163. }
  164. /**
  165. * @brief 非阻塞的方式入队,如果队列已满,直接返回
  166. *
  167. */
  168. template<typename T>
  169. bool RingQueue<T>::push_NoBlock(const T& value)
  170. {
  171. {
  172. // std::unique_lock<std::mutex> lock(m_mutex, std::defer_lock);
  173. // if(!lock.try_lock())
  174. // {
  175. // return false;
  176. // }
  177. std::lock_guard<std::mutex> lock(m_mutex);
  178. /* 先检查队列是否还有剩余空间 */
  179. if(isFull())
  180. {
  181. return false;
  182. }
  183. else if(m_rear == -1)
  184. {
  185. m_front = 0;
  186. m_rear = 0;
  187. }
  188. m_queue[m_rear] = value;
  189. m_rear = (m_rear + 1) % m_capacity;
  190. }
  191. m_cond_NoEmpty.notify_all();
  192. return true;
  193. }
  194. template<typename T>
  195. bool RingQueue<T>::push_NoBlock(T&& value)
  196. {
  197. {
  198. // std::unique_lock<std::mutex> lock(m_mutex, std::defer_lock);
  199. // if(!lock.try_lock())
  200. // {
  201. // return false;
  202. // }
  203. std::lock_guard<std::mutex> lock(m_mutex);
  204. /* 先检查队列是否还有剩余空间 */
  205. if(isFull())
  206. {
  207. return false;
  208. }
  209. else if(m_rear == -1)
  210. {
  211. m_front = 0;
  212. m_rear = 0;
  213. }
  214. m_queue[m_rear] = std::move(value);
  215. m_rear = (m_rear + 1) % m_capacity;
  216. }
  217. m_cond_NoEmpty.notify_all();
  218. return true;
  219. }
  220. /**
  221. * @brief 出队,删除队列的首个元素
  222. * 注意,如果存储的是指针,需要手动释放该指针指向的内存区域,不然会造成内存泄漏
  223. *
  224. * @tparam T
  225. */
  226. template<typename T>
  227. void RingQueue<T>::pop()
  228. {
  229. {
  230. std::unique_lock<std::mutex> lock(m_mutex);
  231. if(isEmpty())
  232. {
  233. return;
  234. }
  235. m_front = (m_front + 1) % m_capacity;
  236. if(m_front == m_rear)
  237. {
  238. m_front = -1;
  239. m_rear = -1;
  240. }
  241. }
  242. m_cond_NoFull.notify_all();
  243. }
  244. /* 获取队列中第一个值,但是不出队
  245. * 阻塞的方式获取,如果队列为空,会一直阻塞住,直到获取到数据为止 */
  246. template<typename T>
  247. T RingQueue<T>::front()
  248. {
  249. T retValue;
  250. {
  251. std::unique_lock<std::mutex> lock(m_mutex);
  252. m_cond_NoEmpty.wait(lock, [this](){
  253. return (!isEmpty() || m_isExit);
  254. });
  255. if(m_isExit)
  256. {
  257. return retValue;
  258. }
  259. retValue = m_queue[m_front];
  260. }
  261. return retValue;
  262. }
  263. /* 获取队列中第一个值,但是不出队,非阻塞的方式获取 */
  264. template<typename T>
  265. bool RingQueue<T>::front_NoBlock(T& t)
  266. {
  267. {
  268. std::unique_lock<std::mutex> lock(m_mutex);
  269. if(isEmpty())
  270. {
  271. return false;
  272. }
  273. t = m_queue[m_front];
  274. }
  275. return true;
  276. }
  277. /* 获取对立第一个数据,获取完立刻出队
  278. * 如果队列为空,会阻塞住,直到有数据为止 */
  279. template<typename T>
  280. T&& RingQueue<T>::front_pop()
  281. {
  282. T ret;
  283. {
  284. std::unique_lock<std::mutex> lock(m_mutex);
  285. m_cond_NoEmpty.wait(lock, [this](){
  286. return (!isEmpty() || m_isExit);
  287. });
  288. if(m_isExit)
  289. {
  290. return std::move(ret);
  291. }
  292. ret = std::move(m_queue[m_front]);
  293. m_front = (m_front + 1) % m_capacity;
  294. if(m_front == m_rear)
  295. {
  296. m_front = -1;
  297. m_rear = -1;
  298. }
  299. }
  300. m_cond_NoFull.notify_all();
  301. return std::move(ret);
  302. }
  303. template<typename T>
  304. bool RingQueue<T>::front_pop_NoBlock(T& t)
  305. {
  306. {
  307. std::unique_lock<std::mutex> lock(m_mutex);
  308. if(isEmpty())
  309. {
  310. return false;
  311. }
  312. t = std::move(m_queue[m_front]);
  313. m_front = (m_front + 1) % m_capacity;
  314. if(m_front == m_rear)
  315. {
  316. m_front = -1;
  317. m_rear = -1;
  318. }
  319. }
  320. m_cond_NoFull.notify_all();
  321. return true;
  322. }
  323. /**
  324. * @brief 设置队列大小
  325. * 注意:使用这个设置,如果队列中存储的是指针,指针的内存区域需要在调用这个函数之前释放,不然可能会造成
  326. * 内存泄漏
  327. *
  328. * @tparam T
  329. * @param size
  330. */
  331. template<typename T>
  332. void RingQueue<T>::setQueueCapacity(long size)
  333. {
  334. if(m_queue != nullptr)
  335. {
  336. delete[] m_queue;
  337. m_queue = nullptr;
  338. }
  339. m_capacity = size;
  340. m_front = -1;
  341. m_rear = -1;
  342. m_queue = new T[m_capacity];
  343. }
  344. /* 获取队列中有效值的大小 */
  345. template<typename T>
  346. long RingQueue<T>::QueueSize()
  347. {
  348. std::lock_guard<std::mutex> lock(m_mutex);
  349. if(m_rear == -1)
  350. {
  351. return 0;
  352. }
  353. else if(m_rear > m_front)
  354. {
  355. return m_rear - m_front;
  356. }
  357. else if(m_rear < m_front)
  358. {
  359. return m_capacity - ( m_front - m_rear );
  360. }
  361. /* 这时候是队列满 */
  362. return m_capacity;
  363. }
  364. /* 获取队列容量 */
  365. template<typename T>
  366. long RingQueue<T>::QueueCapacity()
  367. {
  368. std::lock_guard<std::mutex> lock(m_mutex);
  369. return m_capacity;
  370. }
  371. /**
  372. * @brief 判断队列是否为空
  373. *
  374. * @tparam T
  375. * @return true
  376. * @return false
  377. */
  378. template<typename T>
  379. bool RingQueue<T>::isEmpty()
  380. {
  381. if((m_front == m_rear) && (m_front == -1))
  382. {
  383. return true;
  384. }
  385. return false;
  386. }
  387. /**
  388. * @brief 判断队列是否已满,这里判断依赖入队和出队后的队头和队尾指针的位置
  389. * 1、队头和队尾指针相等,但是队尾指针不等于-1,表示队列已满
  390. *
  391. * @tparam T
  392. * @return true
  393. * @return false
  394. */
  395. template<typename T>
  396. bool RingQueue<T>::isFull()
  397. {
  398. /* 如果m_rear或者m_front不等于-1,说明此时里面有内容
  399. * 同时m_front == m_rear,队列就满了 */
  400. if(m_front == m_rear && m_rear != -1)
  401. {
  402. return true;
  403. }
  404. return false;
  405. }
  406. /* 退出所有可能的阻塞函数 */
  407. template<typename T>
  408. void RingQueue<T>::exit()
  409. {
  410. m_isExit = true;
  411. m_cond_NoFull.notify_all();
  412. m_cond_NoEmpty.notify_all();
  413. }
  414. #endif /* RINGQUEUE_H */