TDME2  1.9.200
Queue.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <tdme/tdme.h>
6 
7 #include <queue>
8 
11 
12 using std::queue;
13 
16 
17 namespace tdme {
18 namespace os {
19 namespace threading {
20 
21 /**
22  * Consumer/producer queue.
23  * @author Andreas Drewke
24  */
25 template <typename T>
26 class Queue {
27 public:
28  // forbid class copy
30 
31  /**
32  * @brief Public constructor
33  */
34  Queue(const unsigned int maxElements) :
36  m("queue"),
37  c("queue"),
38  stopRequested(false) {
39  //
40  }
41 
42  /**
43  * @brief Destructor, removes remaining elements from queue
44  */
45  virtual ~Queue() {
46  while (data.size() > 0) {
47  auto element = data.front();
48  delete element;
49  data.pop();
50  }
51  }
52 
53  /**
54  * @brief Requests this queue to be stopped, any gets will be woke up and return NULL
55  */
56  void stop() {
57  stopRequested = true;
58  c.broadcast();
59  }
60 
61  /**
62  * @brief Gets an element from this queue, if no element exists yet the calling thread will be blocked until an element is available
63  * @return T*
64  */
65  T* getElement() {
66  m.lock();
67  while (data.empty() && stopRequested == false) {
68  c.wait(m);
69  }
70  if (stopRequested == true && data.size() == 0) {
71  m.unlock();
72  c.signal();
73  return NULL;
74  } else {
75  auto element = data.front();
76  data.pop();
77  m.unlock();
78  return element;
79  }
80  }
81 
82  /**
83  * @brief Adds an element to this queue, signals threads which waits for an element
84  * @param element T* element
85  * @param declinable bool if element is declinable
86  * @return if element was added
87  */
88  bool addElement(T* element, const bool declinable) {
89  m.lock();
90  if (declinable == true && data.size() > maxElements) {
91  m.unlock();
92  return false;
93  }
94  data.push(element);
95  c.signal();
96  m.unlock();
97  return true;
98  }
99 
100 protected:
101  typedef queue<T*> QueueType;
103  unsigned int maxElements;
104 
105 private:
108  volatile bool stopRequested;
109 
110 };
111 
112 };
113 };
114 };
Threading condition variable implementation.
Definition: Condition.h:23
void broadcast()
wake ups all waiting threads on this condition, associated mutex should protect broadcast
Definition: Condition.h:49
void signal()
wake ups a waiting thread on this condition, associated mutex should protect signal
Definition: Condition.h:42
void wait(Mutex &mutex)
Blocks current thread until signaled/broadcasted, associated mutex should protect wait.
Definition: Condition.h:56
Mutex implementation.
Definition: Mutex.h:19
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47
Consumer/producer queue.
Definition: Queue.h:26
T * getElement()
Gets an element from this queue, if no element exists yet the calling thread will be blocked until an...
Definition: Queue.h:65
queue< T * > QueueType
Definition: Queue.h:101
bool addElement(T *element, const bool declinable)
Adds an element to this queue, signals threads which waits for an element.
Definition: Queue.h:88
unsigned int maxElements
Definition: Queue.h:103
volatile bool stopRequested
Definition: Queue.h:108
void stop()
Requests this queue to be stopped, any gets will be woke up and return NULL.
Definition: Queue.h:56
virtual ~Queue()
Destructor, removes remaining elements from queue.
Definition: Queue.h:45
Definition: fwd-tdme.h:4
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6