Ethereum  PoC-8
The C++ Implementation of Ethereum
concurrent_queue.h
Go to the documentation of this file.
1 // Aleth: Ethereum C++ client, tools and libraries.
2 // Copyright 2018 Aleth Authors.
3 // Licensed under the GNU General Public License, Version 3.
4 
5 #pragma once
6 
7 #include "Exceptions.h"
8 
9 #include <condition_variable>
10 #include <mutex>
11 #include <queue>
12 #include <utility>
13 
14 namespace dev
15 {
20 template <typename _T, typename _QueueT = std::queue<_T>>
22 {
23 public:
24  template <typename _U>
25  void push(_U&& _elem)
26  {
27  {
28  std::lock_guard<decltype(x_mutex)> guard{x_mutex};
29  m_queue.push(std::forward<_U>(_elem));
30  }
31  m_cv.notify_one();
32  }
33 
34  _T pop()
35  {
36  std::unique_lock<std::mutex> lock{ x_mutex };
37  m_cv.wait(lock, [this] { return !m_queue.empty(); });
38  auto item = std::move(m_queue.front());
39  m_queue.pop();
40  return item;
41  }
42 
43  _T pop(std::chrono::milliseconds const& _waitDuration)
44  {
45  std::unique_lock<std::mutex> lock{x_mutex};
46  if (!m_cv.wait_for(lock, _waitDuration, [this] { return !m_queue.empty(); }))
47  BOOST_THROW_EXCEPTION(WaitTimeout());
48 
49  auto item = std::move(m_queue.front());
50  m_queue.pop();
51  return item;
52  }
53 
54 private:
55  _QueueT m_queue;
56  std::mutex x_mutex;
57  std::condition_variable m_cv;
58 };
59 
60 } // namespace dev
dev::concurrent_queue::pop
_T pop()
Definition: concurrent_queue.h:34
dev::concurrent_queue
Definition: concurrent_queue.h:22
Exceptions.h
dev::concurrent_queue::push
void push(_U &&_elem)
Definition: concurrent_queue.h:25
dev
Definition: Address.cpp:21
dev::concurrent_queue::pop
_T pop(std::chrono::milliseconds const &_waitDuration)
Definition: concurrent_queue.h:43