/*
 *  TnThreadSafeQueue.h
 *  OpenGL Map Engine
 *
 *  Created by Thant on 9/17/2010
 *  Copyright 2010 TeleNav Inc. All rights reserved.
 *
 */

#ifndef TN_THREAD_SAFE_QUEUE_H
#define TN_THREAD_SAFE_QUEUE_H

#include <queue>
#include <boost/optional.hpp>
#include "foundation/Locks.h"
#include "foundation/Mutex.h"

template <typename T>
class TnThreadSafeQueue
{
public:
    
    TnThreadSafeQueue() {}
    TnThreadSafeQueue(TnThreadSafeQueue const & queue) : m_queue(queue.m_queue) {}

    boost::optional<T> pop() {
        Tn::Foundation::lock_guard<Tn::Foundation::mutex> guard(m_mutex);
        boost::optional<T> result;
        if (!m_queue.empty())
        {
            result = m_queue.front();
            m_queue.pop();
        }
        return result;
    }

    void push(T const & t) {
        Tn::Foundation::lock_guard<Tn::Foundation::mutex> guard(m_mutex);
        m_queue.push(t);
    }

private:
    std::queue<T> m_queue;
    Tn::Foundation::mutex m_mutex;
};


#endif
