ProducerConsumerQueue
folly/ProducerConsumerQueue.h
The folly::ProducerConsumerQueue
class is a one-producer one-consumer queue with very low synchronization overhead.
The queue must be created with a fixed maximum size (and allocates that many cells of sizeof(T)), and it provides just a few simple operations:
read
: Attempt to read the value at the front to the queue into a variable, returnsfalse
iff queue was empty.write
: Emplace a value at the end of the queue, returnsfalse
iff the queue was full.frontPtr
: Retrieve a pointer to the item at the front of the queue, ornullptr
if it is empty.popFront
: Remove the item from the front of the queue (queue must not be empty).isEmpty
: Check if the queue is empty.isFull
: Check if the queue is full.sizeGuess
: Returns the number of entries in the queue. Because of the way we coordinate threads, this guess could be slightly wrong when called by the producer/consumer thread, and it could be wildly inaccurate if called from any other threads. Hence, only call from producer/consumer threads!
All of these operations are wait-free. The read operations (including frontPtr
and popFront
) and write operations must only be called by the reader and writer thread, respectively. isFull
, isEmpty
, and sizeGuess
may be called by either thread, but the return values from read
, write
, or frontPtr
are sufficient for most cases.
write
may fail if the queue is full, and read
may fail if the queue is empty, so in many situations it is important to choose the queue size such that the queue filling or staying empty for long is unlikely.
Example
A toy example that doesn't really do anything useful:
folly::ProducerConsumerQueue<folly::fbstring> queue{size}; std::thread reader([&queue] {
for (;;) {
folly::fbstring str;
while (!queue.read(str)) {
//spin until we get a value
continue;
} sink(str);
}
}); // producer thread:
for (;;) {
folly::fbstring str = source();
while (!queue.write(str)) {
//spin until the queue has room
continue;
}
}
Alternatively, the consumer may be written as follows to use the 'front' value in place, thus avoiding moves or copies:
std::thread reader([&queue] {
for (;;) {
folly::fbstring* pval;
do {
pval = queue.frontPtr();
} while (!pval); // spin until we get a value; sink(*pval);
queue.popFront();
}
});
ProducerConsumerQueue的更多相关文章
- C#中的线程(二) 线程同步基础
1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具: 简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...
- 细说.NET中的多线程 (五 使用信号量进行同步)
上一节主要介绍了使用锁进行同步,本节主要介绍使用信号量进行同步 使用EventWaitHandle信号量进行同步 EventWaitHandle主要用于实现信号灯机制.信号灯主要用于通知等待的线程.主 ...
- C#中的多线程 - 同步基础
原文:http://www.albahari.com/threading/part2.aspx 文章来源:http://blog.gkarch.com/threading/part2.html 1同步 ...
- 使用事件等待句柄EventWaitHandler 实现生产者、消费者队列
using System; using System.Threading; using System.Collections.Generic; class ProducerConsumerQueue ...
- C#学习笔记之线程 - 通知Signal
通知事件等待句柄 Signal With EventWaitHandle 事件等待句柄常用于通知.当一个线程等待直到接收到另外一个线程发出的信号.事件等待句柄是最简单的信号结构,它与C#事件无关.有三 ...
- C#中的线程(中)-线程同步
1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具: 简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...
- C# 多线程学习笔记 - 2
本文主要针对 GKarch 相关文章留作笔记,仅在原文基础上记录了自己的理解与摘抄部分片段. 遵循原作者的 CC 3.0 协议. 如果想要了解更加详细的文章信息内容,请访问下列地址进行学习. 原文章地 ...
- trinitycore 魔兽服务器源码分析(三) 多线程相关
先看LockedQueue.h template <class T, typename StorageType = std::deque<T> >class LockedQue ...
- Java 多线程学习笔记:生产者消费者问题
前言:最近在学习Java多线程,看到ImportNew网上有网友翻译的一篇文章<阻塞队列实现生产者消费者模式>.在文中,使用的是Java的concurrent包中的阻塞队列来实现.在看完后 ...
随机推荐
- [置顶]
kubernetes1.7新特性:日志审计变化
背景概念 出于安全方面的考虑,Kubernetes提供了日志审计记录,用来记录不同普通用户.管理员和系统中各个组件的日志信息. Kubernetes日志审计是Kube-apiserver组件的一部分功 ...
- wordpress 生成自定义 meta box
工具 https://jeremyhixon.com/tool/wordpress-meta-box-generator/ 使用 生成代码 /** * Generated by the WordPre ...
- java 导入Excel -- 套路及代码分析
一.思路分析 1.我们要做导入,实际上也就是先文件上传,然后读取文件的数据. 2.我们要有一个导入的模板,因为我们导入的Excel列要和我们的数据字段匹配上,所以我们要给它来一个规定,也就是模板. 3 ...
- git学习笔记——查看git历史记录
1.查看Git日志: 命令:git log 默认不加参数,git日志会按照最新的日期从上往下显示 参数:-p 显示版本间的代码差异 -数字 显示部分的提交 -哈希值 显示指定的版本 2.指定查找范围: ...
- Python学习-字典练习:简单通讯录
功能要求: 查询联系人,输入姓名,可以查询当前通讯录里面的联系人信息,若联系人存在,则输出联系人信息,若不存在,则告知 插入联系人,可以向通讯录中新建联系人,若联系人已经存在,则询问是否修改联系人信息 ...
- C++ set容器
STL中的set容器的一点总结:(元素唯一,且排序) 1.关于set (头文件:<set>) C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, strin ...
- 让thinkphp 5 支持pathinfo 的 nginx ,去掉index.php
在TP5.0中查阅tp5官方文档,注意:5.0取消了URL模式的概念,并且普通模式的URL访问不再支持.phthinfo 是什么? PHP中的全局变量$_SERVER['PATH_INFO']是一个很 ...
- 《DSP using MATLAB》示例 Example 9.4
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 接口测试基础——第7篇 简单的Python知识普及(二)之装饰器
今天我们来学习python里面的“装饰器” 1.我在函数test运行前想先打印一些内容怎么办? def func(param): print u"打印的内容" para ...
- 收藏一下mybatis全局参数配置
http://blog.csdn.net/shaoduo/article/details/54285981