#ifndef _THREADSAFEQUEUE_H_ #define _THREADSAFEQUEUE_H_ #include extern "C" { #include } // This class is a queue that is safe to use from multiple threads at once. template class ThreadSafeQueue { public: ThreadSafeQueue(); virtual ~ThreadSafeQueue(); void push(T& item); T pop(); private: pthread_mutex_t m; std::queue q; }; template ThreadSafeQueue::ThreadSafeQueue() { pthread_mutex_init(&m,NULL); } template ThreadSafeQueue::~ThreadSafeQueue() { pthread_mutex_destroy(&m); } template void ThreadSafeQueue::push(T& item) { pthread_mutex_lock(&m); q.push(item); pthread_mutex_unlock(&m); } template T ThreadSafeQueue::pop() { pthread_mutex_lock(&m); T item = q.front(); q.pop(); pthread_mutex_unlock(&m); return item; } #endif // _THREADSAFEQUEUE_H_