main
Benjamin Kraft 2 years ago
parent 78252c94e3
commit e81a883bda
  1. 15
      src/Queue.h

@ -23,23 +23,15 @@ class Queue {
} }
}; };
virtual void update() {} virtual void update() {}
// First in: points to next free slot
// First in
size_t tail = 0; size_t tail = 0;
// First out: points to leaving element position
// First out
size_t head = 0; size_t head = 0;
T * container[MAX_QUEUE_SIZE]; T * container[MAX_QUEUE_SIZE];
protected: protected:
bool isEmpty() const { bool isEmpty() const {
return tail == head; return tail == head;
} }
size_t size() const {
if (tail > head)
return tail - head;
return MAX_QUEUE_SIZE - head + tail;
}
T * pop(){ T * pop(){
mutex.lock(); mutex.lock();
T * element = container[head]; T * element = container[head];
@ -53,8 +45,11 @@ public:
} }
void push(T * element){ void push(T * element){
mutex.lock(); mutex.lock();
if ((tail + 1) % MAX_QUEUE_SIZE != head){
container[tail] = element; container[tail] = element;
tail = (tail + 1) % MAX_QUEUE_SIZE; tail = (tail + 1) % MAX_QUEUE_SIZE;
} else
std::cout << "Queue is full!\n";
mutex.unlock(); mutex.unlock();
} }
}; };

Loading…
Cancel
Save