BlockingQueue.h
Go to the documentation of this file.
1 
20 #ifndef BLOCKINGQUEUE_H_4LEVMY0N
21 #define BLOCKINGQUEUE_H_4LEVMY0N
22 
23 #include "uscxml/Common.h"
24 #include <list>
25 #include <condition_variable>
26 
27 namespace uscxml {
28 
29 template <class T>
31 public:
32  BlockingQueue() {}
33  virtual ~BlockingQueue() {
34  }
35 
36  virtual void push(const T& elem) {
37  std::lock_guard<std::mutex> lock(_mutex);
38  _queue.push_back(elem);
39  _cond.notify_all();
40  }
41 
42  virtual void push_front(const T& elem) {
43  std::lock_guard<std::mutex> lock(_mutex);
44  _queue.push_front(elem);
45  _cond.notify_all();
46  }
47 
48  virtual T pop() {
49  std::lock_guard<std::mutex> lock(_mutex);
50 // std::cout << "Popping from " << this << std::endl;
51  while (_queue.empty()) {
52  _cond.wait(_mutex);
53  }
54  T ret = _queue.front();
55  _queue.pop_front();
56  return ret;
57  }
58 
59  virtual void clear() {
60  std::lock_guard<std::mutex> lock(_mutex);
61  _queue.clear();
62  }
63 
64  virtual bool isEmpty() {
65  std::lock_guard<std::mutex> lock(_mutex);
66  return _queue.empty();
67  }
68 
69 protected:
70  std::mutex _mutex;
71  std::condition_variable_any _cond;
72  std::list<T> _queue;
73 };
74 
75 }
76 
77 #endif /* end of include guard: BLOCKINGQUEUE_H_4LEVMY0N */
Definition: Breakpoint.cpp:26
Definition: BlockingQueue.h:30