cnstream_queue.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef CNSTREAM_THREADSAFE_QUEUE_HPP_
  21. #define CNSTREAM_THREADSAFE_QUEUE_HPP_
  22. #include <condition_variable>
  23. #include <mutex>
  24. #include <queue>
  25. namespace cnstream {
  26. template <typename T>
  27. class ThreadSafeQueue {
  28. public:
  29. ThreadSafeQueue() = default;
  30. ThreadSafeQueue(const ThreadSafeQueue& other) = delete;
  31. ThreadSafeQueue& operator=(const ThreadSafeQueue& other) = delete;
  32. bool TryPop(T& value);
  33. void WaitAndPop(T& value);
  34. bool WaitAndTryPop(T& value, const std::chrono::microseconds rel_time);
  35. void Push(const T& new_value);
  36. bool Empty() {
  37. std::lock_guard<std::mutex> lk(data_m_);
  38. return q_.empty();
  39. }
  40. uint32_t Size() {
  41. std::lock_guard<std::mutex> lk(data_m_);
  42. return q_.size();
  43. }
  44. private:
  45. std::mutex data_m_;
  46. std::queue<T> q_;
  47. std::condition_variable notempty_cond_;
  48. };
  49. template <typename T>
  50. bool ThreadSafeQueue<T>::TryPop(T& value) {
  51. std::lock_guard<std::mutex> lk(data_m_);
  52. if (q_.empty()) {
  53. return false;
  54. } else {
  55. value = q_.front();
  56. q_.pop();
  57. return true;
  58. }
  59. }
  60. template <typename T>
  61. void ThreadSafeQueue<T>::WaitAndPop(T& value) {
  62. std::unique_lock<std::mutex> lk(data_m_);
  63. notempty_cond_.wait(lk, [&] { return !q_.empty(); });
  64. value = q_.front();
  65. q_.pop();
  66. }
  67. template <typename T>
  68. bool ThreadSafeQueue<T>::WaitAndTryPop(T& value, const std::chrono::microseconds rel_time) {
  69. std::unique_lock<std::mutex> lk(data_m_);
  70. if (notempty_cond_.wait_for(lk, rel_time, [&] { return !q_.empty(); })) {
  71. value = q_.front();
  72. q_.pop();
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. template <typename T>
  79. void ThreadSafeQueue<T>::Push(const T& new_value) {
  80. std::unique_lock<std::mutex> lk(data_m_);
  81. q_.push(new_value);
  82. lk.unlock();
  83. notempty_cond_.notify_one();
  84. }
  85. } // namespace cnstream
  86. #endif // CNSTREAM_THREADSAFE_QUEUE_HPP_