SHARE
TWEET

Untitled

a guest Jan 14th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <utility>
  3. #include <mutex>
  4. #include <queue>
  5.  
  6. template <typename T> class Queue {
  7.  public:
  8.   T Pop();
  9.   size_t Size();
  10.  
  11.   template <typename U>
  12.   void Push(U var);
  13.  
  14.   template <typename... U>
  15.   void Emplace(U &&... args);
  16.  private:
  17.   std::queue<T> data;
  18.   std::mutex access;
  19. };
  20.  
  21. template <typename T>
  22. inline T Queue<T>::Pop() {
  23.   std::lock_guard<std::mutex> mutex_lock(access);
  24.   T res = std::move(data.front());
  25.   data.pop();
  26.   return res;
  27. }
  28.  
  29. template <typename T>
  30. inline size_t Queue<T>::Size() {
  31.   std::lock_guard<std::mutex> mutex_lock(access);
  32.   return data.size();
  33. }
  34.  
  35. template <typename T>
  36. template <typename U>
  37. inline void Queue<T>::Push(U value) {
  38.   access.lock();
  39.   data.push(std::move(value));
  40.   access.unlock();
  41. }
  42.  
  43. template <typename T>
  44. template <typename... U>
  45. inline void Queue<T>::Emplace(U &&... args) {
  46.   access.lock();
  47.   data.emplace(std::forward<U &&...>(args...));
  48.   access.unlock();
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top