42 #ifndef _DWMCONCURRENTQUEUE_HH_ 43 #define _DWMCONCURRENTQUEUE_HH_ 102 useconds_t pushSleepUsecs = 1000)
103 : _length(0), _maxLength(maxLength),
104 _pushSleepUsecs(pushSleepUsecs)
106 _front = _back =
new Entry(T());
107 _pushingLocked = _poppingLocked =
false;
115 while (_front !=
nullptr) {
129 while (_poppingLocked.exchange(
true));
130 if (_front->next !=
nullptr) {
132 Entry *oldFirst = _front;
133 _front = _front->next;
134 result = _front->value;
135 _poppingLocked =
false;
142 _poppingLocked =
false;
149 bool PopFront(std::vector<T> & result)
151 typename std::vector<T>::size_type numEntries = 0;
153 while (_poppingLocked.exchange(
true));
154 if (_front->next !=
nullptr) {
156 Entry *oldFront = _front;
160 while (_front->next !=
nullptr) {
161 _front = _front->next;
164 _length -= numEntries;
168 Entry *newFront = _front;
170 _poppingLocked =
false;
172 result.resize(numEntries);
174 typename std::vector<T>::size_type i = 0;
175 Entry *entry = oldFront->next;
176 while (entry != newFront) {
177 Entry *delEntry = entry;
178 result[i] = entry->value;
187 _poppingLocked =
false;
197 while (_length > _maxLength) { usleep(_pushSleepUsecs); };
198 Entry *tmp =
new Entry(t);
199 while (_pushingLocked.exchange(
true));
202 _pushingLocked =
false;
210 bool PushBack(
const std::vector<T> & t)
212 while (_length > _maxLength) { usleep(_pushSleepUsecs); }
213 std::vector<Entry *> newEntries(t.size());
214 typename std::vector<T>::size_type i = 0;
215 newEntries[i] =
new Entry(t[i]);
217 for (; i < t.size(); ++i) {
218 newEntries[i] =
new Entry(t[i]);
219 newEntries[i-1]->next = newEntries[i];
221 while (_pushingLocked.exchange(
true));
222 _back->next = newEntries[0];
223 _back = newEntries[t.size() - 1];
224 _pushingLocked =
false;
237 : value(val), next(
nullptr)
240 std::atomic<Entry *> next;
244 std::atomic<bool> _poppingLocked;
246 std::atomic<bool> _pushingLocked;
247 std::atomic<uint64_t> _length;
249 useconds_t _pushSleepUsecs;
257 #endif // _DWMCONCURRENTQUEUE_HH_ ~ConcurrentQueue()
Destructor. Walks the queue and deletes all entries.
Definition: DwmConcurrentQueue.hh:113
bool PushBack(const T &t)
Waits for the queue to not be full (see the Queue constructor), then pushes t onto the back of the qu...
Definition: DwmConcurrentQueue.hh:195
ConcurrentQueue(uint64_t maxLength=10000, useconds_t pushSleepUsecs=1000)
Constructor.
Definition: DwmConcurrentQueue.hh:101
bool PopFront(T &result)
If the queue is not empty, pops the front entry into result and returns true.
Definition: DwmConcurrentQueue.hh:127
Definition: DwmBZ2IO.hh:67
Concurrent queue template.
Definition: DwmConcurrentQueue.hh:92