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, returnsfalseiff queue was empty.write: Emplace a value at the end of the queue, returnsfalseiff the queue was full.frontPtr: Retrieve a pointer to the item at the front of the queue, ornullptrif 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包中的阻塞队列来实现.在看完后 ...
随机推荐
- 创建Azure Function
azure function的用途在于运行一些逻辑简单的执行逻辑,比如batch job,定时任务,webhook等等.1. 创建azure function创建完毕后,进入app service,选 ...
- chrome 55 zepto tap事件出错?
刚才升级chrome后发现的,在54.0.2840.98上点击没有问题,在新升级的55.0.2883.75 上点击后会报错Cannot read property 'trigger' of undef ...
- 什么是URL,URI或URN?
什么是URI? 每个Web服务器资源都有一个名字,这样客户端就可以说明它们感兴趣的资源是什么了. 服务器资源名被称为统一资源标识符(Uniform Resource Identifier, URI). ...
- caffe 卷积层的运算
贾清扬寻找快速算法之路:https://github.com/Yangqing/caffe/wiki/Convolution-in-Caffe:-a-memo 卷积运算图文并茂:http://www. ...
- c语言符号常量与常变量的区别?
<blockquote>定义符号常量:#define PI 3.1415926 //注意后面没有分号 定义常变量 :const float PI=3.1415 ...
- ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...
- sql中的一些函数(长期更新。。)
前言 在最近看别人的sql的时候,看到一些函数,比如left(),right()等等,好奇是什么意思,查询之后觉得还是挺有用的,特此记录下来.博客会在遇到新的函数的时候定期更新. 正文 1. left ...
- (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
每次使用 Visual Studio 的模板创建一个 UWP 程序,我们会在项目中发现大量的项目文件.配置.应用启动流程代码和界面代码.然而这些文件在 UWP 程序中到底是如何工作起来的? 我从零开始 ...
- piwik docker 安装
备注: 生产环境使用docker-compose 1. 安装docker && docker-compose 此处略过 2. 下载docker-compose 的文件 h ...
- CentOS下编译安装LNMP环境
一.卸载系统预安装的LAMP软件 rpm -qa|grep httpd rpm -e httpd httpd-tools rpm -qa|grep mysql rpm -e mysql mysql-l ...