RingQueue.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. #define _DefaultValue (m_isUseDefaultValue.load() ? m_defaultValue : T{})
  33. template<typename T>
  34. class RingQueue
  35. {
  36. RingQueue(const RingQueue<T>& queue) = delete;
  37. RingQueue<T> operator=(const RingQueue<T>& queue) = delete;
  38. public:
  39. RingQueue();
  40. RingQueue(long size);
  41. RingQueue(long size, T defaultValue);
  42. ~RingQueue();
  43. /* 入队,默认是阻塞入队 */
  44. void push(const T& value);
  45. void push(T&& value);
  46. bool push_noBlock(const T& value);
  47. bool push_noBlock(T&& value);
  48. /* 入队,队列满就出队一个,非阻塞 */
  49. T push_pop(const T& value);
  50. /* 出队,删除队列的首个元素
  51. * 注意,如果存储的是指针,需要手动释放该指针指向的内存区域,不然会造成内存泄漏 */
  52. void pop();
  53. /* 获取队列中第一个值),但是不出队
  54. * 阻塞的方式获取,如果队列为空,会一直阻塞住,直到获取到数据为止 */
  55. T front();
  56. /* 非阻塞的方式获取,队列为空返回false */
  57. bool front_noBlock(T& t);
  58. /* 非阻塞方式获取第一个值,如果对列为空,则会返回设置的默认值 */
  59. T front_noBlock();
  60. /* 获取对立第一个数据,获取完立刻出队
  61. * 如果队列为空,会阻塞住,直到有数据为止
  62. * 如果删除了拷贝构造函数,使用会报错 */
  63. T front_pop();
  64. /* 非阻塞方式获取第一个值,并出队 */
  65. bool front_pop_noBlock(T& t);
  66. /* 非阻塞方式获取第一个值,并出队,如果队列为空,会返回设置的默认值 */
  67. T front_pop_noBlock();
  68. /* 通过移动语义获取数据,获取完成后队列中的数据只是个空壳了,因此直接就出队了 */
  69. bool front_pop_move(T& t);
  70. bool front_pop_move_noBlock(T& t);
  71. /* 设置队列大小 */
  72. void setQueueCapacity(long size);
  73. /* 设置默认值,给指针类型使用,如果是非阻塞获取,空的时候可以返回为设置的默认值(如nullptr) */
  74. void setDefaultValue(T defaultValue);
  75. /* 获取队列大小,队列中有效值的大小 */
  76. long QueueSize();
  77. /* 获取队列容量 */
  78. long QueueCapacity();
  79. /* 判断队列是否为空 */
  80. bool isEmpty();
  81. /* 判断队列是否已满 */
  82. bool isFull();
  83. /* 清空队列 */
  84. void clearQueue();
  85. /* 退出所有可能的阻塞函数 */
  86. void exit();
  87. private:
  88. /* 判断是否空 */
  89. inline bool _isEmpty();
  90. /* 判断是否满 */
  91. inline bool _isFull();
  92. private:
  93. std::atomic_bool m_isExit = false; /* 是否退出,这个标识位是为了退出阻塞住的函数 */
  94. std::mutex m_mutex; /* 互斥锁 */
  95. T m_defaultValue; /* 默认值 */
  96. std::atomic_bool m_isUseDefaultValue = false; /* 是否使用默认值 */
  97. T* m_queue = nullptr; /* 队列 */
  98. long m_capacity = 0; /* 队列容量 */
  99. long m_front = -1; /* 队头 */
  100. long m_rear = -1; /* 队尾 */
  101. std::condition_variable m_cond_NoFull; /* 非满条件变量 */
  102. std::condition_variable m_cond_NoEmpty; /* 非空条件变量 */
  103. };
  104. /* =====================================================================
  105. * ***************************** 函数实现 *****************************
  106. * ===================================================================== */
  107. /* 这个构造函数需要调用 setQueueSize 设置环形队列的大小 */
  108. template<typename T>
  109. RingQueue<T>::RingQueue()
  110. {
  111. }
  112. template<typename T>
  113. RingQueue<T>::RingQueue(long capacity)
  114. {
  115. m_capacity = capacity;
  116. m_queue = new T[m_capacity] {};
  117. }
  118. /* 添加默认值 */
  119. template<typename T>
  120. RingQueue<T>::RingQueue(long capacity, T defaultValue)
  121. {
  122. m_capacity = capacity;
  123. m_queue = new T[m_capacity] {};
  124. for(long i = 0; i < m_capacity; i++)
  125. {
  126. m_queue[i] = defaultValue;
  127. }
  128. m_defaultValue = defaultValue;
  129. m_isUseDefaultValue.store(true); // 设置使用默认值
  130. }
  131. template<typename T>
  132. RingQueue<T>::~RingQueue()
  133. {
  134. if(m_queue != nullptr)
  135. {
  136. delete[] m_queue;
  137. m_queue = nullptr;
  138. }
  139. }
  140. /* 清空队列 */
  141. template<typename T>
  142. void RingQueue<T>::clearQueue()
  143. {
  144. std::lock_guard<std::mutex> lock(m_mutex);
  145. if(m_queue != nullptr)
  146. {
  147. delete[] m_queue;
  148. m_queue = nullptr;
  149. }
  150. m_front = -1;
  151. m_rear = -1;
  152. }
  153. /*************** 入队 *******************/
  154. template<typename T>
  155. void RingQueue<T>::push(const T& value)
  156. {
  157. {
  158. std::unique_lock<std::mutex> _lock(m_mutex);
  159. m_cond_NoFull.wait(_lock, [this]() {
  160. return (!_isFull() || m_isExit);
  161. });
  162. if(m_isExit)
  163. {
  164. return;
  165. }
  166. if(m_rear == -1)
  167. {
  168. m_front = 0;
  169. m_rear = 0;
  170. }
  171. m_queue[m_rear] = value;
  172. m_rear = (m_rear + 1) % m_capacity;
  173. }
  174. m_cond_NoEmpty.notify_all();
  175. }
  176. template<typename T>
  177. void RingQueue<T>::push(T&& value)
  178. {
  179. {
  180. std::unique_lock<std::mutex> lock(m_mutex);
  181. m_cond_NoFull.wait(lock, [this](){
  182. return (!_isFull() || m_isExit);
  183. });
  184. if(m_isExit)
  185. {
  186. return;
  187. }
  188. if(m_rear == -1)
  189. {
  190. m_front = 0;
  191. m_rear = 0;
  192. }
  193. m_queue[m_rear] = std::move(value);
  194. m_rear = (m_rear + 1) % m_capacity;
  195. }
  196. m_cond_NoEmpty.notify_all();
  197. }
  198. /**
  199. * @brief 非阻塞的方式入队,如果队列已满,直接返回
  200. *
  201. */
  202. template<typename T>
  203. bool RingQueue<T>::push_noBlock(const T& value)
  204. {
  205. {
  206. // std::unique_lock<std::mutex> lock(m_mutex, std::defer_lock);
  207. // if(!lock.try_lock())
  208. // {
  209. // return false;
  210. // }
  211. std::lock_guard<std::mutex> lock(m_mutex);
  212. /* 先检查队列是否还有剩余空间 */
  213. if(_isFull())
  214. {
  215. return false;
  216. }
  217. else if(m_rear == -1)
  218. {
  219. m_front = 0;
  220. m_rear = 0;
  221. }
  222. m_queue[m_rear] = value;
  223. m_rear = (m_rear + 1) % m_capacity;
  224. }
  225. m_cond_NoEmpty.notify_all();
  226. return true;
  227. }
  228. template<typename T>
  229. bool RingQueue<T>::push_noBlock(T&& value)
  230. {
  231. {
  232. // std::unique_lock<std::mutex> lock(m_mutex, std::defer_lock);
  233. // if(!lock.try_lock())
  234. // {
  235. // return false;
  236. // }
  237. std::lock_guard<std::mutex> lock(m_mutex);
  238. /* 先检查队列是否还有剩余空间 */
  239. if(_isFull())
  240. {
  241. return false;
  242. }
  243. else if(m_rear == -1)
  244. {
  245. m_front = 0;
  246. m_rear = 0;
  247. }
  248. m_queue[m_rear] = std::move(value);
  249. m_rear = (m_rear + 1) % m_capacity;
  250. }
  251. m_cond_NoEmpty.notify_all();
  252. return true;
  253. }
  254. /* 入队,队列满就出队一个,非阻塞 */
  255. template<typename T>
  256. T RingQueue<T>::push_pop(const T& value)
  257. {
  258. T ret = _DefaultValue;
  259. {
  260. std::unique_lock<std::mutex> lock(m_mutex);
  261. /* 如果队列已满,先出队一个元素 */
  262. if(_isFull())
  263. {
  264. ret = std::move(m_queue[m_front]);
  265. m_front = (m_front + 1) % m_capacity;
  266. if(m_front == m_rear)
  267. {
  268. m_front = -1;
  269. m_rear = -1;
  270. }
  271. }
  272. /* 入队新元素 */
  273. if(m_rear == -1)
  274. {
  275. m_front = 0;
  276. m_rear = 0;
  277. }
  278. m_queue[m_rear] = value;
  279. m_rear = (m_rear + 1) % m_capacity;
  280. }
  281. m_cond_NoEmpty.notify_all();
  282. return ret;
  283. }
  284. /**
  285. * @brief 出队,删除队列的首个元素
  286. * 注意,如果存储的是指针,需要手动释放该指针指向的内存区域,不然会造成内存泄漏
  287. *
  288. * @tparam T
  289. */
  290. template<typename T>
  291. void RingQueue<T>::pop()
  292. {
  293. {
  294. std::unique_lock<std::mutex> lock(m_mutex);
  295. if(_isEmpty())
  296. {
  297. return;
  298. }
  299. m_front = (m_front + 1) % m_capacity;
  300. if(m_front == m_rear)
  301. {
  302. m_front = -1;
  303. m_rear = -1;
  304. }
  305. }
  306. m_cond_NoFull.notify_all();
  307. }
  308. /* 获取队列中第一个值,但是不出队
  309. * 阻塞的方式获取,如果队列为空,会一直阻塞住,直到获取到数据为止 */
  310. template<typename T>
  311. T RingQueue<T>::front()
  312. {
  313. std::unique_lock<std::mutex> lock(m_mutex);
  314. m_cond_NoEmpty.wait(lock, [this](){
  315. return (!isEmpty() || m_isExit);
  316. });
  317. if(m_isExit)
  318. {
  319. return _DefaultValue;
  320. }
  321. return m_queue[m_front];
  322. }
  323. /* 获取队列中第一个值,但是不出队,非阻塞的方式获取 */
  324. template<typename T>
  325. bool RingQueue<T>::front_noBlock(T& t)
  326. {
  327. {
  328. std::unique_lock<std::mutex> lock(m_mutex);
  329. if(_isEmpty())
  330. {
  331. return false;
  332. }
  333. t = m_queue[m_front];
  334. }
  335. return true;
  336. }
  337. /* 非阻塞方式获取第一个值,如果对列为空,则会返回设置的默认值 */
  338. template<typename T>
  339. T RingQueue<T>::front_noBlock()
  340. {
  341. std::unique_lock<std::mutex> lock(m_mutex);
  342. if(_isEmpty())
  343. {
  344. return _DefaultValue;
  345. }
  346. return m_queue[m_front];
  347. }
  348. /* 获取对立第一个数据,获取完立刻出队
  349. * 如果队列为空,会阻塞住,直到有数据为止 */
  350. template<typename T>
  351. T RingQueue<T>::front_pop()
  352. {
  353. // T ret = _DefaultValue;
  354. {
  355. std::unique_lock<std::mutex> lock(m_mutex);
  356. m_cond_NoEmpty.wait(lock, [this](){
  357. return (!_isEmpty() || m_isExit);
  358. });
  359. /* 是否退出 */
  360. if(m_isExit)
  361. {
  362. return _DefaultValue;
  363. }
  364. /* 临时记录索引 */
  365. long front = m_front;
  366. m_front = (m_front + 1) % m_capacity;
  367. if(m_front == m_rear)
  368. {
  369. m_front = -1;
  370. m_rear = -1;
  371. }
  372. m_cond_NoFull.notify_all();
  373. return m_queue[front];
  374. }
  375. }
  376. /* 非阻塞方式获取第一个值,并出队 */
  377. template<typename T>
  378. bool RingQueue<T>::front_pop_noBlock(T& t)
  379. {
  380. {
  381. std::unique_lock<std::mutex> lock(m_mutex);
  382. if(_isEmpty())
  383. {
  384. return false;
  385. }
  386. t = std::move(m_queue[m_front]);
  387. m_front = (m_front + 1) % m_capacity;
  388. if(m_front == m_rear)
  389. {
  390. m_front = -1;
  391. m_rear = -1;
  392. }
  393. }
  394. m_cond_NoFull.notify_all();
  395. return true;
  396. }
  397. /* 非阻塞方式获取第一个值,并出队 */
  398. template<typename T>
  399. T RingQueue<T>::front_pop_noBlock()
  400. {
  401. // T ret = _DefaultValue;
  402. {
  403. std::unique_lock<std::mutex> lock(m_mutex);
  404. // m_cond_NoEmpty.wait(lock, [this](){
  405. // return (!_isEmpty() || m_isExit);
  406. // });
  407. if(_isEmpty())
  408. {
  409. return _DefaultValue;
  410. }
  411. /* 临时记录索引 */
  412. long front = m_front;
  413. m_front = (m_front + 1) % m_capacity;
  414. if(m_front == m_rear)
  415. {
  416. m_front = -1;
  417. m_rear = -1;
  418. }
  419. m_cond_NoFull.notify_all();
  420. return m_queue[front];
  421. }
  422. }
  423. /* 通过移动语义获取数据,获取完成后队列中的数据只是个空壳了,因此直接就出队了 */
  424. template<typename T>
  425. bool RingQueue<T>::front_pop_move(T& t)
  426. {
  427. std::unique_lock<std::mutex> lock(m_mutex);
  428. m_cond_NoEmpty.wait(lock, [this]() {
  429. return (!_isEmpty() || m_isExit);
  430. });
  431. /* 是否退出 */
  432. if(m_isExit)
  433. {
  434. return false;
  435. }
  436. t = std::move(m_queue[m_front]); // 移动语义获取数据
  437. m_front = (m_front + 1) % m_capacity;
  438. if(m_front == m_rear)
  439. {
  440. m_front = -1;
  441. m_rear = -1;
  442. }
  443. m_cond_NoFull.notify_all();
  444. return true;
  445. }
  446. template<typename T>
  447. bool RingQueue<T>::front_pop_move_noBlock(T& t)
  448. {
  449. std::unique_lock<std::mutex> lock(m_mutex);
  450. // m_cond_NoEmpty.wait(lock, [this](){
  451. // return (!_isEmpty() || m_isExit);
  452. // });
  453. if(_isEmpty())
  454. {
  455. return false;
  456. }
  457. t = std::move(m_queue[m_front]); // 移动语义获取数据
  458. m_front = (m_front + 1) % m_capacity;
  459. if(m_front == m_rear)
  460. {
  461. m_front = -1;
  462. m_rear = -1;
  463. }
  464. m_cond_NoFull.notify_all();
  465. return true;
  466. }
  467. /**
  468. * @brief 设置队列大小
  469. * 注意:使用这个设置,如果队列中存储的是指针,指针的内存区域需要在调用这个函数之前释放,不然可能会造成
  470. * 内存泄漏
  471. *
  472. * @tparam T
  473. * @param size
  474. */
  475. template<typename T>
  476. void RingQueue<T>::setQueueCapacity(long size)
  477. {
  478. if(m_queue != nullptr)
  479. {
  480. delete[] m_queue;
  481. m_queue = nullptr;
  482. }
  483. m_capacity = size;
  484. m_front = -1;
  485. m_rear = -1;
  486. m_queue = new T[m_capacity];
  487. }
  488. /* 设置默认值,给指针类型使用,如果是非阻塞获取,空的时候可以返回为设置的默认值(如nullptr) */
  489. template<typename T>
  490. void RingQueue<T>::setDefaultValue(T defaultValue)
  491. {
  492. m_defaultValue = defaultValue;
  493. m_isUseDefaultValue.store(true); // 设置使用默认值
  494. }
  495. /* 获取队列中有效值的大小 */
  496. template<typename T>
  497. long RingQueue<T>::QueueSize()
  498. {
  499. std::lock_guard<std::mutex> lock(m_mutex);
  500. if(m_rear == -1)
  501. {
  502. return 0;
  503. }
  504. else if(m_rear > m_front)
  505. {
  506. return m_rear - m_front;
  507. }
  508. else if(m_rear < m_front)
  509. {
  510. return m_capacity - ( m_front - m_rear );
  511. }
  512. /* 这时候是队列满 */
  513. return m_capacity;
  514. }
  515. /* 获取队列容量 */
  516. template<typename T>
  517. long RingQueue<T>::QueueCapacity()
  518. {
  519. std::lock_guard<std::mutex> lock(m_mutex);
  520. return m_capacity;
  521. }
  522. /**
  523. * @brief 判断队列是否为空
  524. *
  525. * @tparam T
  526. * @return true
  527. * @return false
  528. */
  529. template<typename T>
  530. bool RingQueue<T>::isEmpty()
  531. {
  532. std::lock_guard<std::mutex> lock(m_mutex);
  533. return _isEmpty();
  534. }
  535. /**
  536. * @brief 判断队列是否已满,这里判断依赖入队和出队后的队头和队尾指针的位置
  537. * 1、队头和队尾指针相等,但是队尾指针不等于-1,表示队列已满
  538. *
  539. * @tparam T
  540. * @return true
  541. * @return false
  542. */
  543. template<typename T>
  544. bool RingQueue<T>::isFull()
  545. {
  546. std::lock_guard<std::mutex> lock(m_mutex);
  547. return _isFull();
  548. }
  549. /* 退出所有可能的阻塞函数 */
  550. template<typename T>
  551. void RingQueue<T>::exit()
  552. {
  553. m_isExit = true;
  554. m_cond_NoFull.notify_all();
  555. m_cond_NoEmpty.notify_all();
  556. }
  557. /* 判断是否空 */
  558. template<typename T>
  559. bool RingQueue<T>::_isEmpty()
  560. {
  561. if((m_front == m_rear) && (m_front == -1))
  562. {
  563. return true;
  564. }
  565. return false;
  566. }
  567. /* 判断是否满 */
  568. template<typename T>
  569. inline bool RingQueue<T>::_isFull()
  570. {
  571. /* 如果m_rear或者m_front不等于-1,说明此时里面有内容
  572. * 同时m_front == m_rear,队列就满了 */
  573. if(m_front == m_rear && m_rear != -1)
  574. {
  575. return true;
  576. }
  577. return false;
  578. }
  579. #endif /* RINGQUEUE_H */