本文主要是介绍base6. BlockingQueue无界缓冲区模板类、BoundedBlockingQueue有界缓冲区模板类——生产者与消费者模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
BlockingQueue类图
数据成员:
mutable MutexLock mutex_:一个MutexLock类型的锁mutex_
Condition notEmpty_:一个Condition类型的信号量notEmpty_,用于通知队列非空
std::deque queue_:一个类型T的队列
成员函数:
BlockingQueue():构造函数初始化数据成员
void put(const T& x):生产者生产产品,并通知消费者队列已有产品
T take():消费者消费产品
size_t size() const:返回队列queue_的大小
BlockingQueue.h
//无界缓冲区,生产者与消费者模型
#ifndef MUDUO_BASE_BLOCKINGQUEUE_H
#define MUDUO_BASE_BLOCKINGQUEUE_H#include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h>#include <boost/noncopyable.hpp>
#include <deque>
#include <assert.h>namespace muduo
{template<typename T>
class BlockingQueue : boost::noncopyable
{public://构造函数初始化数据成员BlockingQueue(): mutex_(),notEmpty_(mutex_),queue_(){}//生产者生产产品void put(const T& x){MutexLockGuard lock(mutex_);queue_.push_back(x);//通知消费者队列已有产品notEmpty_.notify(); // TODO: move outside of lock}//消费者消费产品T take(){MutexLockGuard lock(mutex_);// always use a while-loop, due to spurious wakeupwhile (queue_.empty()){notEmpty_.wait();}assert(!queue_.empty());T front(queue_.front());queue_.pop_front();return front;}//返回队列queue_的大小size_t size() const{MutexLockGuard lock(mutex_);return queue_.size();}private://一个MutexLock类型的锁mutex_mutable MutexLock mutex_;//一个Condition类型的信号量notEmpty_,用于通知队列非空Condition notEmpty_;//一个类型T的队列std::deque<T> queue_;
};}#endif // MUDUO_BASE_BLOCKINGQUEUE_H
BoundedBlockingQueue类图
数据成员:
mutable MutexLock mutex_:一个MutexLock类型的锁mutex_
Condition notEmpty_:一个Condition类型的信号量notEmpty_,用于通知队列非空
Condition notFull_:一个Condition类型的信号量notFull_,用于通知队列未满
boost::circular_buffer queue_:一个类型T的环形缓冲区queue_,为有界缓冲区
成员函数:
BlockingQueue():构造函数初始化数据成员
void put(const T& x):生产者从queue_尾部生产产品,并通知消费者队列已有产品
T take():消费者消费产品,消费者从queue_头部消费产品,并通知生产者队列未满
bool empty() const:判断队列queue_是否是空的
bool full() const:判断队列queue_是否是满的
size_t size() const:返回queue_中当前产品的个数
size_t capacity() const:返回queue_总的容量
BoundedBlockingQueue.h
//有界缓冲区,生产者与消费者模型
#ifndef MUDUO_BASE_BOUNDEDBLOCKINGQUEUE_H
#define MUDUO_BASE_BOUNDEDBLOCKINGQUEUE_H#include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h>#include <boost/circular_buffer.hpp>
#include <boost/noncopyable.hpp>
#include <assert.h>namespace muduo
{template<typename T>
class BoundedBlockingQueue : boost::noncopyable
{public:explicit BoundedBlockingQueue(int maxSize): mutex_(),notEmpty_(mutex_),notFull_(mutex_),queue_(maxSize){}//生产者生产产品void put(const T& x){MutexLockGuard lock(mutex_);while (queue_.full()){notFull_.wait();}assert(!queue_.full());//生产者从queue_尾部生产产品queue_.push_back(x);notEmpty_.notify(); // TODO: move outside of lock}//消费者消费产品T take(){MutexLockGuard lock(mutex_);while (queue_.empty()){notEmpty_.wait();}assert(!queue_.empty());//消费者从queue_头部消费产品T front(queue_.front());queue_.pop_front();notFull_.notify(); // TODO: move outside of lockreturn front;}//判断队列queue_是否是空的bool empty() const{MutexLockGuard lock(mutex_);return queue_.empty();}//判断队列queue_是否是满的bool full() const{MutexLockGuard lock(mutex_);return queue_.full();}//返回queue_中当前产品的个数size_t size() const{MutexLockGuard lock(mutex_);return queue_.size();}//返回queue_总的容量size_t capacity() const{MutexLockGuard lock(mutex_);return queue_.capacity();}private:mutable MutexLock mutex_;Condition notEmpty_;//一个Condition类型的信号量notFull_,用于通知队列未满Condition notFull_;//一个环形缓冲区queue_,为有界缓冲区boost::circular_buffer<T> queue_;
};}#endif // MUDUO_BASE_BOUNDEDBLOCKINGQUEUE_H
这篇关于base6. BlockingQueue无界缓冲区模板类、BoundedBlockingQueue有界缓冲区模板类——生产者与消费者模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!