BoundedBlockingQueue

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值