消息队列

    添加时间:2013-6-6 点击量:

    一个哄骗前提变量写的消息队列,基于双缓冲的,固然比拟三缓冲的差距不小,然则还是值得拿来进修一下前提变量。



    /
    
    BufQueue.h

    Created on: May 30, 2013
    Author: archy_yu
    /

    #ifndef BUFQUEUE_H_
    #define BUFQUEUE_H_

    #include
    <list>
    #include
    <pthread.h>
    //#include CommonStruct.h

    typedef
    void CommonItem;

    #define BATPROCESS_NUM 5

    class BufQueue
    {
    public:

    BufQueue();

    virtual ~BufQueue();

    int peek(CommonItem &item);

    int append(CommonItem item);

    std::list
    <CommonItem> serial_read(std::list<CommonItem> inlist);

    int set_read_event();

    void WaitReadEventByTimeOut(bool& isReadable);

    private:

    std::list
    <CommonItem> _write_list;

    std::list
    <CommonItem> _read_list;

    pthread_mutex_t _write_mutex;

    pthread_mutex_t _read_mutex;

    pthread_cond_t _read_cond;

    };

    #endif / BUFQUEUE_H_ /



    /
    
    BufQueue.cpp

    Created on: May 30, 2013
    Author: archy_yu
    /

    #include
    BufQueue.h
    #include
    <sys/time.h>
    #include
    <time.h>

    void maketimeout(struct timespec tsp,long ns = 1
    {
    struct timeval now;

    //get the current time
    gettimeofday(&now,0);
    tsp
    ->tv_sec = now.tv_sec;
    tsp
    ->tv_nsec = now.tv_usec 1000; //usec to nsec
    tsp->tv_nsec += 1000ns;

    }

    CommonItem PopMsgToPutFromList( std::list
    <CommonItem> pList )
    {
    if(pList == NULL)
    {
    return NULL;
    }

    if(pList->empty())
    {
    return NULL;
    }
    else
    {
    CommonItem item
    = pList->front();
    pList
    ->pop_front();
    return item;
    }

    }


    BufQueue::BufQueue()
    {
    pthread_mutex_init(
    &this->_write_mutex,NULL);
    pthread_mutex_init(
    &this->_read_mutex,NULL);
    pthread_cond_init(
    &this->_read_cond,NULL);

    this->_read_list = new std::list<CommonItem>();
    this->_write_list = new std::list<CommonItem>();

    }

    BufQueue::
    ~BufQueue()
    {
    pthread_mutex_destroy(
    &this->_write_mutex);
    pthread_mutex_destroy(
    &this->_read_mutex);
    pthread_cond_destroy(
    &this->_read_cond);

    this->_read_list->clear();
    this->_write_list->clear();

    }

    int BufQueue::peek(CommonItem &item)
    {
    pthread_mutex_lock(
    &this->_write_mutex);
    item
    = PopMsgToPutFromList(this->_read_list);
    pthread_mutex_unlock(
    &this->_write_mutex);

    return 0;
    }

    int BufQueue::append(CommonItem item)
    {
    if(item == NULL)
    {
    return 0;
    }

    bool issetread = false;

    pthread_mutex_lock(
    &this->_write_mutex);
    this->_write_list->push_back(item);
    issetread
    = (this->_write_list->size() > BATPROCESS_NUM);
    pthread_mutex_unlock(
    &this->_write_mutex);

    if(issetread)
    {
    this->set_read_event();
    }

    return 0;
    }

    std::list
    <CommonItem> BufQueue::serial_read(std::list<CommonItem> inQueue)
    {
    pthread_mutex_lock(
    &this->_write_mutex);

    std::list
    <CommonItem> tmplist = this->_write_list;
    this->_write_list = this->_read_list;
    this->_read_list = tmplist;

    tmplist
    = this->_read_list;
    this->_read_list = inQueue;

    pthread_mutex_unlock(
    &this->_write_mutex);
    return tmplist;
    }

    int BufQueue::set_read_event()
    {
    pthread_mutex_lock(
    &this->_read_mutex);
    pthread_cond_signal(
    &this->_read_cond);
    pthread_mutex_unlock(
    &this->_read_mutex);
    return 0;
    }

    void BufQueue::WaitReadEventByTimeOut(bool& isReadable)
    {
    pthread_mutex_lock(
    &this->_read_mutex);
    struct timespec ts;
    maketimeout(
    &ts,0);
    isReadable
    = (0 == pthread_cond_timedwait(&this->_read_cond,&this->_read_mutex, &ts));
    pthread_mutex_unlock(
    &this->_read_mutex);
    }


    无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
    分享到: