MarlinMT  0.1.0
Queue.h
Go to the documentation of this file.
1 #ifndef MARLINMT_CONCURRENCY_QUEUE_h
2 #define MARLINMT_CONCURRENCY_QUEUE_h 1
3 
4 // -- std headers
5 #include <mutex>
6 #include <utility>
7 #include <queue>
8 
9 namespace marlinmt {
10 
11  namespace concurrency {
12 
19  template <
20  typename T,
22  class Queue {
23  public:
24  Queue() = default ;
25  ~Queue() = default ;
26  Queue(const Queue&) = delete ;
27  Queue& operator=(const Queue&) = delete ;
28 
34  Queue( std::size_t maxsize ) {
35  _maxSize = maxsize ;
36  }
37 
45  bool push(T &value) {
46  std::unique_lock<std::mutex> lock(_mutex) ;
47  if( _queue.size() >= _maxSize ) {
48  return false ;
49  }
50  _queue.push( std::move(value) ) ;
51  return true ;
52  }
53 
60  bool pop( T & value ) {
61  std::unique_lock<std::mutex> lock(_mutex) ;
62  if( _queue.empty() ) {
63  return false ;
64  }
65  value = std::move(_queue.front()) ;
66  _queue.pop() ;
67  return true ;
68  }
69 
73  bool empty() const {
74  std::unique_lock<std::mutex> lock(_mutex) ;
75  return _queue.empty() ;
76  }
77 
81  std::size_t maxSize() const {
82  std::unique_lock<std::mutex> lock(_mutex) ;
83  return _maxSize ;
84  }
85 
93  std::size_t setMaxSize( std::size_t maxsize ) {
94  std::unique_lock<std::mutex> lock(_mutex) ;
95  std::swap( _maxSize, maxsize ) ;
96  return maxsize ;
97  }
98 
102  bool isFull() const {
103  std::unique_lock<std::mutex> lock(_mutex) ;
104  return (_queue.size() >= _maxSize) ;
105  }
106 
110  void clear() {
111  std::unique_lock<std::mutex> lock(_mutex) ;
112  while( not _queue.empty() ) {
113  _queue.pop() ;
114  }
115  }
116 
120  std::size_t freeSlots() const {
121  std::unique_lock<std::mutex> lock(_mutex) ;
122  return (_queue.size() >= _maxSize ? 0 : (_maxSize - _queue.size())) ;
123  }
124 
125  private:
127  std::queue<T> _queue {} ;
129  mutable std::mutex _mutex {} ;
131  std::size_t _maxSize {std::numeric_limits<std::size_t>::max()} ;
132  };
133 
134  } // end namespace concurrency
135 
136 } // end namespace marlinmt
137 
138 #endif
std::mutex _mutex
The synchronization mutex.
Definition: Queue.h:129
std::size_t setMaxSize(std::size_t maxsize)
Set the maximum queue size.
Definition: Queue.h:93
std::size_t _maxSize
The maximum size of the queue.
Definition: Queue.h:131
void clear()
Clear the queue.
Definition: Queue.h:110
constexpr unsigned long long value(const Flag_t &flag)
Definition: Flags.h:106
bool isFull() const
Check whether the queue has reached the maximum allowed size.
Definition: Queue.h:102
Queue & operator=(const Queue &)=delete
std::size_t maxSize() const
Get the maximum queue size.
Definition: Queue.h:81
std::queue< T > _queue
The underlying queue object.
Definition: Queue.h:127
std::size_t freeSlots() const
Get the number of free slots in the queue.
Definition: Queue.h:120
Queue(std::size_t maxsize)
Constructor.
Definition: Queue.h:34
bool push(T &value)
Push a value to the queue.
Definition: Queue.h:45
bool pop(T &value)
Pop and get the front element in the queue.
Definition: Queue.h:60
bool empty() const
Whether the queue is empty.
Definition: Queue.h:73