Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ts_queue.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. MMO Client/Server Framework using ASIO
  3. "Happy Birthday Mrs Javidx9!" - javidx9
  4. Videos:
  5. Part #1: https://youtu.be/2hNdkYInj4g
  6. Part #2: https://youtu.be/UbjxGvrDrbw
  7. License (OLC-3)
  8. ~~~~~~~~~~~~~~~
  9. Copyright 2018 - 2020 OneLoneCoder.com
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. 1. Redistributions or derivations of source code must retain the above
  14. copyright notice, this list of conditions and the following disclaimer.
  15. 2. Redistributions or derivative works in binary form must reproduce
  16. the above copyright notice. This list of conditions and the following
  17. disclaimer must be reproduced in the documentation and/or other
  18. materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its
  20. contributors may be used to endorse or promote products derived
  21. from this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. Links
  34. ~~~~~
  35. YouTube: https://www.youtube.com/javidx9
  36. Discord: https://discord.gg/WhwHUMV
  37. Twitter: https://www.twitter.com/javidx9
  38. Twitch: https://www.twitch.tv/javidx9
  39. GitHub: https://www.github.com/onelonecoder
  40. Homepage: https://www.onelonecoder.com
  41. Author
  42. ~~~~~~
  43. David Barr, aka javidx9, �OneLoneCoder 2019, 2020
  44. */
  45. #pragma once
  46. #include <queue>
  47. #include <mutex>
  48. #include <condition_variable>
  49. /*
  50. Threadsafe Queue to be accessed from different threads
  51. */
  52. template <typename T>
  53. class ts_queue
  54. {
  55. public:
  56. ts_queue() = default;
  57. ts_queue(const ts_queue<T>&) = delete;
  58. virtual ~ts_queue() { clear(); }
  59. public:
  60. // Returns and maintains item at front of Queue
  61. const T& front()
  62. {
  63. std::scoped_lock lock(muxQueue);
  64. return queue.front();
  65. }
  66. // Returns and maintains item at back of Queue
  67. const T& back()
  68. {
  69. std::scoped_lock lock(muxQueue);
  70. return queue.back();
  71. }
  72. // Removes and returns the first item from Queue
  73. T pop_front()
  74. {
  75. std::scoped_lock lock(muxQueue);
  76. auto t = std::move(queue.front());
  77. queue.pop();
  78. return t;
  79. }
  80. // Adds an item to front of Queue
  81. void push(const T& item)
  82. {
  83. std::scoped_lock lock(muxQueue);
  84. queue.push(std::move(item));
  85. std::unique_lock<std::mutex> ul(muxBlocking);
  86. cvBlocking.notify_one();
  87. }
  88. // Returns true if Queue has no items
  89. bool empty()
  90. {
  91. std::scoped_lock lock(muxQueue);
  92. return queue.empty();
  93. }
  94. // Returns number of items in Queue
  95. size_t size()
  96. {
  97. std::scoped_lock lock(muxQueue);
  98. return queue.size();
  99. }
  100. // Clears Queue
  101. void clear()
  102. {
  103. std::scoped_lock lock(muxQueue);
  104. std::queue<T> empty;
  105. std::swap(queue, empty);
  106. }
  107. void wait()
  108. {
  109. while (empty())
  110. {
  111. std::unique_lock<std::mutex> ul(muxBlocking);
  112. cvBlocking.wait(ul);
  113. }
  114. }
  115. protected:
  116. std::mutex muxQueue;
  117. std::queue<T> queue;
  118. std::condition_variable cvBlocking;
  119. std::mutex muxBlocking;
  120. };