后端程序员之路 41、BlockingQueue
BlockingQueue,阻塞队列,常用于实现生产者和消费者模型
特点:
1、队列为空时,取操作会等到队列有数据
2、队列满时,存操作会等到队列可用
基于C++11的阻塞队列简单实现 - Cynric 的博客 - 博客频道 - CSDN.NET
http://blog.csdn.net/cywosp/article/details/9157379
参考java的阻塞队列实现,还可以有以下细节:
1、ArrayBlockingQueue
用数组来存队列里的数据,可以避免用链表时额外的node对象创建销毁开销
2、LinkedBlockingQueue
最常用,队列可以有无限容量,但是生产速度过快会爆掉内存
3、DelayQueue
node只有延迟时间到了才能取到,存操作不会阻塞
4、PriorityBlockingQueue
类似DelayQueue,传入Compator来决定优先级
5、SynchronousQueue
没有缓冲的等待队列
BlockingQueue - - ITeye技术网站
http://wsmajunfeng.iteye.com/blog/1629354
最终实现的简单BlockingQueue:
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <list>
template<class T> class BlockingQueue {
public:
std::list<T> _queue;
size_t _curr_size;
pthread_mutex_t _lock;
sem_t _consumer_sem;
sem_t _producer_sem;
int _max_size;
public:
BlockingQueue(int max_size) {
pthread_mutex_init(&_lock, NULL);
sem_init(&_consumer_sem, 0, 0);
_max_size = max_size;
_curr_size = 0;
if (max_size > 0) {
sem_init(&_producer_sem, 0, max_size);
}
}
~BlockingQueue() {
pthread_mutex_destroy(&_lock);
sem_destroy(&_consumer_sem);
if (_max_size > 0) {
sem_destroy(&_producer_sem);
}
}
void push(const T& value) {
if (_max_size > 0) {
sem_wait(&_producer_sem);
}
pthread_mutex_lock(&_lock);
_queue.push_back(value);
++_curr_size;
sem_post(&_consumer_sem);
pthread_mutex_unlock(&_lock);
}
void batch_push(const std::vector<T>& values) {
for (uint32_t i = 0; i < values.size(); ++i) {
push( values[i] );
}
}
T take() {
sem_wait(&_consumer_sem);
pthread_mutex_lock(&_lock);
T value = _queue.front();
_queue.pop_front();
--_curr_size;
if (_max_size > 0) {
sem_post(&_producer_sem);
}
pthread_mutex_unlock(&_lock);
return value;
}
bool try_take(T& out) {
int ret = sem_trywait(&_consumer_sem);
if (ret == -1 && errno == EAGAIN) {
return false;
}
pthread_mutex_lock(&_lock);
out = _queue.front();
_queue.pop_front();
--_curr_size;
if (_max_size > 0) {
sem_post(&_producer_sem);
}
pthread_mutex_unlock(&_lock);
return true;
}
size_t size() {
return _curr_size;
}
};
后端程序员之路 41、BlockingQueue的更多相关文章
- 后端程序员之路 59、go uiprogress
gosuri/uiprogress: A go library to render progress bars in terminal applicationshttps://github.com/g ...
- 后端程序员之路 43、Redis list
Redis数据类型之LIST类型 - Web程序猿 - 博客频道 - CSDN.NEThttp://blog.csdn.net/thinkercode/article/details/46565051 ...
- 后端程序员之路 42、Semaphore
前面学习了Pthreads,了解了线程和线程同步,而同步这个东西,与信号量是密不可分的.下面讨论的主要是Pthreads里的semaphore.h,而不是sys/sem.h [Linux]线程同步之信 ...
- 后端程序员之路 33、Index搜索引擎实现分析2-对外接口和大体流程
# index_manager的单例是index server对外的唯一接口,part_indexer是index搜索的核心部分,index_manager持有了一组part_indexer. typ ...
- 后端程序员之路 22、RESTful API
理解RESTful架构 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/09/restful.html RESTful API 设计指南 - 阮一峰的网络日 ...
- 后端程序员之路 16、信息熵 、决策树、ID3
信息论的熵 - guisu,程序人生. 逆水行舟,不进则退. - 博客频道 - CSDN.NEThttp://blog.csdn.net/hguisu/article/details/27305435 ...
- 后端程序员之路 7、Zookeeper
Zookeeper是hadoop的一个子项目,提供分布式应用程序协调服务. Apache ZooKeeper - Homehttps://zookeeper.apache.org/ zookeeper ...
- 后端程序员之路 4、一种monitor的做法
record_t包含_sum._count._time_stamp._max._min最基础的一条记录,可以用来记录最大值.最小值.计数.总和metric_t含有RECORD_NUM(6)份recor ...
- 后端程序员之路 58、go wlog
daviddengcn/go-colortext: Change the color of console text.https://github.com/daviddengcn/go-colorte ...
随机推荐
- 1. Machine Learning - Introduction
Speaker: Andrew Ng 1. Introduction 1.A comptuter program is said to learn from experience E with r ...
- java大数函数(附官方文档链接)
java文档 字段摘要 static BigInteger ONE BigInteger 的常量 1. static BigInteger TEN BigInt ...
- Splits CodeForces - 964A
题意: 我们定义一个不上升的且和为 n 的正整数序列,叫做 n 的分解. 比如, 下面是8的分解: [4, 4], [3, 3, 2], [2, 2, 1, 1, 1, 1], [5, 2, 1]. ...
- CSS 解决Float后塌陷问题
当父级元素没有设定高度时候,而子集元素设定float类型时候,此时父级元素不能靠子集元素撑起来,所以就形成了塌陷: 示例分析 **1.Float之前的效果** <!DOCTYPE html> ...
- 解决springmvc使用@ResponseBody返回String类型字符串中文乱码问题
问题分析: 首先: 确定的是只有当返回值是 String时才会出现中文乱码,而当返回值是Map<String, Object>或者是其它类型时,并没有中文乱码的出现. 然后找原因: 原因是 ...
- ES6 Set All In One
ES6 Set All In One Set 集合 Map 字典/地图 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- 使用 js 实现一个中文自动转换成拼音的工具库
使用 js 实现一个中文自动转换成拼音的工具库 中文 => zhong-wen 应用场景 SEO 友好, URL 自动转换 blogs 发布文章,自动化部署,自动生成 url 的 path (时 ...
- codepen iframe theme id
codepen iframe theme id iframe css theme demos See the Pen css margin collapsing (1. 相邻兄弟元素) by xgqf ...
- UML online tools
UML online tools UML https://www.diagrams.net/assets/svg/home-dia1.svg refs https://www.diagrams.net ...
- Koa 洋葱模型
Koa 洋葱模型 let context = { data: [] }; async function middleware1(ctx, next) { console.log('action 001 ...