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包中的阻塞队列来实现.在看完后 ...
随机推荐
- Buildroot构建指南--Overview
使用Buildroot,让嵌入式Linux系统构建更加便捷.本文以Buildroot-2016.05的版本为基础来讲解,不同版本之间有细节差异,需要根据读者使用的版本自行调整. Buildroot是什 ...
- js 要写严格模式
js 为了能在移动端通用,要写严格模式: 这里多了个逗号,在pc上浏览器可以通过,但是在手机端就不能.
- IOS开发 多线程编程 - NSThread
每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...
- vue 之 vue-router
官方文档 // 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件. // 可以从其他文件 import 进来 c ...
- BZOJ4561 JLoi2016 圆的异或并 【扫描线】【set】*
BZOJ4561 JLoi2016 圆的异或并 Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面积并.异或面积并为:当一片区 ...
- 20179223《Linux内核原理与分析》第五周学习笔记
视频内容知识学习 一.用户态.内核态和中断 1.内核态:处于高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态 2.用户态:处于低的执行级别下,代码只能在级别允许的特定 ...
- WinForm窗体继承自定义的模板窗体出错
在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.而今天自定义一个窗体,然后子窗体继承的时候出现了一点问题. 问题: 在 ...
- 定义文档兼容性、让IE按指定版本解析我们的页面
http://blog.useasp.net/archive/2013/05/29/x-UA-compatible-defining-document-compatibility-emulate-ie ...
- 洛谷 P3802 小魔女帕琪
传送门 题目大意:7个东西,每个有ai个,只有选7次 把7个东西都选到了才行. 题解:7!排列数*每次选择的概率 代码: #include<iostream> #include<cs ...
- 使用PHP类库PHPqrCode生成二维码
PHPqrCode是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo, 查看地址:http://phpqrcode.sourceforge.net/. 下载官 ...