GNU Radio Manual and C++ API Reference  3.7.14.0
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
threadsafe_queue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Free Software Foundation, Inc.
3  *
4  * This file is part of GNU Radio
5  *
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef INCLUDED_GR_RUNTIME_RUNTIME_LOG_THREADSAFE_QUEUE_H
23 #define INCLUDED_GR_RUNTIME_RUNTIME_LOG_THREADSAFE_QUEUE_H
24 #include <condition_variable>
25 #include <mutex>
26 #include <queue>
27 #include <stdexcept>
28 #include <utility>
29 namespace gr {
30 namespace log {
31 using lock_t = std::unique_lock<std::mutex>;
32 template <typename T>
34 {
35 private:
36  std::queue<T> the_queue;
37  mutable std::mutex the_mutex;
38  std::condition_variable the_condition_variable;
39  bool empty() const
40  {
41  lock_t lock(the_mutex);
42  return the_queue.empty();
43  }
44 
45 public:
46  void push(T&& data) noexcept
47  {
48  lock_t lock(the_mutex);
49  the_queue.push(std::move(data));
50  lock.unlock();
51  the_condition_variable.notify_one();
52  }
53 
54  bool empty() noexcept
55  {
56 
57  lock_t lock(the_mutex);
58  return the_queue.empty();
59  }
60  /*! Blocking popping the front element from the queue
61  */
62  T& pop() noexcept
63  {
64  lock_t lock(the_mutex);
65  while (the_queue.empty()) {
66  the_condition_variable.wait(lock);
67  }
68  auto& temp = the_queue.front();
69  the_queue.pop();
70  return temp;
71  }
73  {
74  lock_t lock(the_mutex);
75  if (the_queue.empty()) {
76  throw std::out_of_range("queue empty");
77  }
78  auto& temp = the_queue.front();
79  the_queue.pop();
80  return temp;
81  }
82 };
83 } // namespace log
84 } // namespace gr
85 #endif /* INCLUDED_GR_RUNTIME_RUNTIME_LOG_THREADSAFE_QUEUE_H */
bool empty() noexcept
Definition: threadsafe_queue.h:54
Definition: threadsafe_queue.h:33
T & pop() noexcept
Definition: threadsafe_queue.h:62
T & pop_or_throw()
Definition: threadsafe_queue.h:72
boost::mutex mutex
Definition: thread.h:48
void push(T &&data) noexcept
Definition: threadsafe_queue.h:46
std::unique_lock< std::mutex > lock_t
Definition: threadsafe_queue.h:31
boost::condition_variable condition_variable
Definition: thread.h:50