C/C++ 線程同步安全隊列簡單實現例子

來源:互聯網
上載者:User

標籤:lis   error   str   amp   overflow   logs   eal   size   res   

#ifndef MUTEXLOCKER_H_INCLUDED#define MUTEXLOCKER_H_INCLUDED#include <pthread.h>#include <stdexcept>class MutexLocker{public:    explicit MutexLocker(pthread_mutex_t *mutex):m_mutex(mutex){        int ret = pthread_mutex_lock(m_mutex);        if(ret != 0){            printf("Lock mutex failed");            throw std::logic_error("Could not lock mutex");        }    }    virtual ~MutexLocker(){        pthread_mutex_unlock(m_mutex);    }private:    pthread_mutex_t *m_mutex;};#endif // MUTEXLOCKER_H_INCLUDED
#ifndef SAFEQUEUE_H_INCLUDED#define SAFEQUEUE_H_INCLUDED#include "MutexLocker.h"#include <pthread.h>#include <list>template <class T>class SafeQueue{public:    SafeQueue(int size = 0){        m_capacity = capacity;        m_total_enqueue_count = 0;        pthread_mutexattr_t attr;        pthread_mutexattr_init(&attr);        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);        pthread_mutex_init(&m_lock, &attr);        pthread_mutexattr_destroy(&attr);    }    ~SafeQueue()    {        int frame_count = m_list.size();        for(int i = 0; i < frame_count; i++) {            m_list.pop_front();        }        pthread_mutex_destroy(&m_lock);    }    void SetCapacity(int capacity)    {        m_capacity = capacity;    }    // return 0 if succeed; -1 if the queue is full    int Enqueue(T node)    {        CTMutexLocker locker(&m_lock);        //pthread_mutex_lock(&m_lock);        if (m_capacity > 0 && Size() >= m_capacity){            //pthread_mutex_unlock(&m_lock);            return -1; // overflow        }        m_list.push_back(node);        m_total_enqueue_count++;        //pthread_mutex_unlock(&m_lock);        return 0;    }    // dequeue a item, and save the result to the @item pointer.    // return 0 if succeed, -1 if the queue is empty;    int Dequeue(T *item, int reserve_len = 0)    {        CTMutexLocker locker(&m_lock);        //*item = NULL;        memset(item, 0, sizeof(T));        int total_count = m_list.size();        if(total_count == 0 || total_count < reserve_len){            return -1;        }        *item = m_list.front();        m_list.pop_front();        //pthread_mutex_unlock(&m_lock);        return 0;    }    int DequeueAll(std::list<T> *out_queue, int reserve_len = 0)    {        CTMutexLocker locker(&m_lock);        if(m_list.size() <= reserve_len){            return 0;        }        T item;        int remove_size = m_list.size() - reserve_len;        while(remove_size > 0){            item  = m_list.front();            m_list.pop_front();            if (out_queue){               out_queue->push_back(item);            }            remove_size--;        }        return 0;    }    int Front(T *item)    {        CTMutexLocker locker(&m_lock);        if(m_list.size() == 0){            return -1;        }        *item = m_list.front();        return 0;    }    int Back(T *item)    {        CTMutexLocker locker(&m_lock);        if(Size() == 0){            return -1;        }        *item = m_list.back();        return 0;    }    int Size()    {        CTMutexLocker locker(&m_lock);        return m_list.size();    }    int Lock()    {        return pthread_mutex_lock(&m_lock);    }    int Unlock()    {        return pthread_mutex_unlock(&m_lock);    }    int RePushFront(T node)    {        CTMutexLocker locker(&m_lock);        if (m_capacity > 0 && Size() >= m_capacity)            return -1; // overflow        m_list.push_front(node);        return 0;    }    int PopBack()    {        CTMutexLocker locker(&m_lock);        m_list.pop_back();        return 0;    }private:    std::list<T>    m_list;    int             m_capacity;    pthread_mutex_t m_lock;    uint64_t        m_total_enqueue_count;};#endif // SAFEQUEUE_H_INCLUDED

 

C/C++ 線程同步安全隊列簡單實現例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.