OpenCPN Partial API docs
Loading...
Searching...
No Matches
atomic_queue.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 by Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <mutex>
26#include <queue>
27
28template <typename T>
30public:
31 size_t size() {
32 std::lock_guard<std::mutex> lock(m_mutex);
33 return m_queue.size();
34 }
35
36 bool empty() {
37 std::lock_guard<std::mutex> lock(m_mutex);
38 return m_queue.empty();
39 }
40
41 const T& front() {
42 std::lock_guard<std::mutex> lock(m_mutex);
43 return m_queue.front();
44 }
45
46 void push(const T& value) {
47 std::lock_guard<std::mutex> lock(m_mutex);
48 m_queue.push(value);
49 }
50
51 void pop() {
52 std::lock_guard<std::mutex> lock(m_mutex);
53 m_queue.pop();
54 }
55
56private:
57 std::queue<T> m_queue;
58 mutable std::mutex m_mutex;
59};