
#ifndef QUEUE_H
#define QUEUE_H

#include <pthread.h>
#include <unistd.h>

class Queue
{
	pthread_mutex_t mutex;
	int *items;
	int n, maxItemsNum;
public:
	Queue(int maxItemsNum)
	{
		pthread_mutex_init(&mutex, NULL);
		items = new int[maxItemsNum];
		n = 0;
		this->maxItemsNum = maxItemsNum;
	}
	void add()
	{
		pthread_mutex_lock(&mutex);
		if (n == maxItemsNum)
		{
			pthread_mutex_unlock(&mutex);
			throw "zu viele Benutzer gleichzeitig";
		}
		items[n++] = getpid();
		pthread_mutex_unlock(&mutex);
	}
	int getRank()
	{
		pthread_mutex_lock(&mutex);
		int i;
		for (i = 0; i < n && items[i] != getpid(); i++);
		pthread_mutex_unlock(&mutex);
		return i;
	}
	void remove()
	{
		pthread_mutex_lock(&mutex);
		int i;
		for (i = 0; i < n && items[i] != getpid(); i++);
		for (; i < n - 1; i++)
			items[i] = items[i + 1];
		n--;
		pthread_mutex_unlock(&mutex);
	}
};

#endif

