BoundedBlockingQueue的类图
---------------------------------------------------------------
-mutex_: MutexLock // 互斥量 |
-notEmppty_:Condition //消费者发送生产者的条件变量 |
-notFull_ : Condition //生产者发送给消费者的条件变量|
-queue_:boost::circular_buffer<T> //虚唤醒队列 |
--------------------------------------
<<create>-BoudedBlockingQueue(maxSize:int) //
+put(x:T) : void //入队列 |
+take():T //出队列
+empty() :bool //判断队列是否为空
+full():bool //判断队列是否满
+size() :size_t //产品的多少
+capacity():size_t //仓库的容量
------------------------------------------------------------
BoudedBlockingQueue的源代码:
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#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>
/**
有界缓冲区 :
Mutex + condition + circular_buffer 实现
**/
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_);
//等待队列有空闲,直到notFull_.notify()信号的唤醒
while (queue_.full())
{
notFull_.wait();
}
//断言队列是否空(原子操作)
assert(!queue_.full());
//
queue_.push_back(x);
//通知
notEmpty_.notify(); // TODO: move outside of lock
}
//把数据从队列中提取出来
T take()
{
//加锁
MutexLockGuard lock(mutex_);
//等待队列有数据,直到notEmpty_.notify()信号的唤醒
while (queue_.empty())
{
notEmpty_.wait();
}
//断言队列是否为空
assert(!queue_.empty());
//出数据
T front(queue_.front());
queue_.pop_front();
//发送notFull_.notify()信号
notFull_.notify(); // TODO: move outside of lock
return front;
}
//判断队列是否为空
bool empty() const
{
//先加锁
MutexLockGuard lock(mutex_);
return queue_.empty();
}
//判断队列是否已满
bool full() const
{
//加锁
MutexLockGuard lock(mutex_);
return queue_.full();
}
//数据的大小
size_t size() const
{
MutexLockGuard lock(mutex_);
return queue_.size();
}
//队列的容量大小
size_t capacity() const
{
MutexLockGuard lock(mutex_);
return queue_.capacity();
}
private:
mutable MutexLock mutex_;
Condition notEmpty_; //两个条件变量
Condition notFull_; //
boost::circular_buffer<T> queue_;
};
}
#endif // MUDUO_BASE_BOUNDEDBLOCKINGQUEUE_H
测试程序:
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#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>
/**
有界缓冲区 :
Mutex + condition + circular_buffer 实现
函数功能,类消费者和生产者
**/
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_);
//等待队列有空闲,直到notFull_.notify()信号的唤醒
while (queue_.full())
{
notFull_.wait();
}
//断言队列是否空(原子操作)
assert(!queue_.full());
//
queue_.push_back(x);
//通知
notEmpty_.notify(); // TODO: move outside of lock
}
//把数据从队列中提取出来
T take()
{
//加锁
MutexLockGuard lock(mutex_);
//等待队列有数据,直到notEmpty_.notify()信号的唤醒
while (queue_.empty())
{
notEmpty_.wait();
}
//断言队列是否为空
assert(!queue_.empty());
//出数据
T front(queue_.front());
queue_.pop_front();
//发送notFull_.notify()信号
notFull_.notify(); // TODO: move outside of lock
return front;
}
//判断队列是否为空
bool empty() const
{
//先加锁
MutexLockGuard lock(mutex_);
return queue_.empty();
}
//判断队列是否已满
bool full() const
{
//加锁
MutexLockGuard lock(mutex_);
return queue_.full();
}
//数据的大小
size_t size() const
{
MutexLockGuard lock(mutex_);
return queue_.size();
}
//队列的容量大小
size_t capacity() const
{
MutexLockGuard lock(mutex_);
return queue_.capacity();
}
private:
mutable MutexLock mutex_;
Condition notEmpty_; //两个条件变量
Condition notFull_; //
boost::circular_buffer<T> queue_;
};
}
#endif // MUDUO_BASE_BOUNDEDBLOCKINGQUEUE_H