KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2020 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
30template <typename T>
32{
33public:
35 {
36 }
37
41 void push( T const& aValue )
42 {
43 GUARD guard( m_mutex );
44 m_queue.push( aValue );
45 }
46
50 void move_push( T&& aValue )
51 {
52 GUARD guard( m_mutex );
53 m_queue.push( std::move( aValue ) );
54 }
55
63 bool pop( T& aReceiver )
64 {
65 GUARD guard( m_mutex );
66
67 if( m_queue.empty() )
68 {
69 return false;
70 }
71 else
72 {
73 aReceiver = std::move( m_queue.front() );
74 m_queue.pop();
75 return true;
76 }
77 }
78
82 bool empty() const
83 {
84 GUARD guard( m_mutex );
85 return m_queue.empty();
86 }
87
91 size_t size() const
92 {
93 GUARD guard( m_mutex );
94 return m_queue.size();
95 }
96
100 void clear()
101 {
102 GUARD guard( m_mutex );
103
104 while( !m_queue.empty() )
105 {
106 m_queue.pop();
107 }
108 }
109
110private:
111 typedef std::lock_guard<std::mutex> GUARD;
112
113 std::queue<T> m_queue;
114 mutable std::mutex m_mutex;
115};
116
117#endif // SYNC_QUEUE_H
Synchronized, locking queue.
Definition: sync_queue.h:32
std::mutex m_mutex
Definition: sync_queue.h:114
bool pop(T &aReceiver)
Pop a value if the queue into the provided variable.
Definition: sync_queue.h:63
bool empty() const
Return true if the queue is empty.
Definition: sync_queue.h:82
void clear()
Clear the queue.
Definition: sync_queue.h:100
std::lock_guard< std::mutex > GUARD
Definition: sync_queue.h:111
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:91
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:41
std::queue< T > m_queue
Definition: sync_queue.h:113
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:50