KiCad PCB EDA Suite
sync_queue.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef SYNC_QUEUE_H
21 #define SYNC_QUEUE_H
22 
23 #include <mutex>
24 #include <queue>
25 
30 template <typename T> class SYNC_QUEUE
31 {
32  typedef std::lock_guard<std::mutex> GUARD;
33 
34  std::queue<T> m_queue;
35  mutable std::mutex m_mutex;
36 
37 public:
39  {
40  }
41 
45  void push( T const& aValue )
46  {
47  GUARD guard( m_mutex );
48  m_queue.push( aValue );
49  }
50 
54  void move_push( T&& aValue )
55  {
56  GUARD guard( m_mutex );
57  m_queue.push( std::move( aValue ) );
58  }
59 
66  bool pop( T& aReceiver )
67  {
68  GUARD guard( m_mutex );
69 
70  if( m_queue.empty() )
71  {
72  return false;
73  }
74  else
75  {
76  aReceiver = std::move( m_queue.front() );
77  m_queue.pop();
78  return true;
79  }
80  }
81 
85  bool empty() const
86  {
87  GUARD guard( m_mutex );
88  return m_queue.empty();
89  }
90 
94  size_t size() const
95  {
96  GUARD guard( m_mutex );
97  return m_queue.size();
98  }
99 
103  void clear()
104  {
105  GUARD guard( m_mutex );
106 
107  while( !m_queue.empty() )
108  {
109  m_queue.pop();
110  }
111  }
112 };
113 
114 #endif // SYNC_QUEUE_H
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:45
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
bool pop(T &aReceiver)
Pop a value off the queue into the provided variable.
Definition: sync_queue.h:66
T
enum T contains all this lexer's tokens.
Synchronized, locking queue.
Definition: sync_queue.h:30
bool empty() const
Return true iff the queue is empty.
Definition: sync_queue.h:85
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:54
std::mutex m_mutex
Definition: sync_queue.h:35
std::lock_guard< std::mutex > GUARD
Definition: sync_queue.h:32
std::queue< T > m_queue
Definition: sync_queue.h:34
void clear()
Clear the queue.
Definition: sync_queue.h:103