C++队列模板使用std::queue

#ifndef _TASK_QUEUE_H_
#define _TASK_QUEUE_H_

#include 
#include 
#include 

template
class TaskQueue 
{
public:
    TaskQueue(){
        queue_.clear();
    }

    virtual ~TaskQueue(){
    };

    /**
    * @Appends the task to the queue
    * @param   the task object
    **/
    virtual void append(const T& _t){
        thr_lck lck(mtx_);
        queue_.push(_t);
    }

    /**
    * @Return the current size of the queue
    * @returns the queue size
    **/
    virtual size_t queueSize() const {
        thr_lck lck(mtx_);
        return queue_.size();
    }

    /**
    * @Return the queue
    * @returns the message queue
    **/
    virtual bool empty() {
        thr_lck lck(mtx_);
        return queue_.empty();
    }

    /**
    * @Return the queue
    * @returns the message queue
    **/
    virtual std::queue& getQueue() {
        thr_lck lck(mtx_);
        return queue_;
    }

    /**
    * @Return the queue
    * @returns the message queue
    **/
    virtual const std::queue& getQueue() const {
        thr_lck lck(mtx_);
        return queue_;
    }

    /**
    * @Return the oldest task from the queue
    * @returns the front task
    **/
    virtual T front() {
        thr_lck lck(mtx_);
        T message;
        if (!queue_.empty()) 
        {
            message = queue_.front();
        }
        return message;
    }

    /**
    * @Pop the oldest task from the front of the queue.
    * @returns the oldest task
    **/
    virtual T popMessage(){
        thr_lck lck(mtx_);
        T message;
        if (!queue_.empty()) 
        {
            message = queue_.front();
            queue_.pop();
        }
        return message;
    }

protected:
    typedef std::queue task_queue;
    task_queue queue_;

    typedef boost::mutex::scoped_lock thr_lck;
    boost::mutex mtx_;
};

#endif

你可能感兴趣的:(C_C++)