#ifndef _THREADSAFEQUEUE_H_
#define _THREADSAFEQUEUE_H_

#include<queue>
extern "C" {
#include<pthread.h>
}

// This class is a queue that is safe to use from multiple threads at once.

template <class T>
class ThreadSafeQueue {
    public:
        ThreadSafeQueue();
        virtual ~ThreadSafeQueue();
        void push(T& item);
        T pop();
    private:
        pthread_mutex_t m;
        std::queue<T> q;
};

template <class T>
ThreadSafeQueue<T>::ThreadSafeQueue() {
    pthread_mutex_init(&m,NULL);
}

template <class T>
ThreadSafeQueue<T>::~ThreadSafeQueue() {
    pthread_mutex_destroy(&m);
}

template <class T>
void ThreadSafeQueue<T>::push(T& item) {
    pthread_mutex_lock(&m);
    q.push(item);
    pthread_mutex_unlock(&m);
}

template <class T>
T ThreadSafeQueue<T>::pop() {
    pthread_mutex_lock(&m);
    T item = q.front();
    q.pop();
    pthread_mutex_unlock(&m);
    return item;
}




#endif // _THREADSAFEQUEUE_H_