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包中的阻塞队列来实现.在看完后 ...
随机推荐
- python删除list中元素的三种方法
a.pop(index):删除列表a中index处的值,并且返回这个值. del(a[index]):删除列表a中index处的值,无返回值. del中的index可以是切片,所以可以实现批量删除. ...
- css3公共样式
温馨提示:一下css封装,建议按需使用,否则会造成很大的代码冗余,且很多样式会造成不符合预期的效果,建议合理使用 <a href="https://meyerweb.com/eric/ ...
- erlang游戏开发tcp
之前在开发游戏的时候我们采用smartfoxserver这个java开发的游戏引擎,这个引擎在开发回合制游戏方面速度还是不错的.但是面对客户日益增长的需求还是有些力不从心.比如集群,比如灾备,热切换, ...
- Netty系列之一开始使用
Netty是用来做什么的呢,我的理解是它是一个网络开发框架,利用它能很快速方便的开发出高性能的服务端和客户端.刚开始学习java的时候你一定接触过怎么利用socket去实现服务端和客户端,后来java ...
- tomcat错误:The page you tried to access (/manager/login.do) does not exist
今天在idea上跑一个项目,所有配置都正常,其他接口测试也正常.唯独“/manage/user”接口在测试的时候后台没反应,也就是程序根本没往下走,postman测试显示如下: 浏览器范围url连接显 ...
- 一些Fibonacci数列的神奇性质【数学】
递推式: \(f_i=1 (1\leq i\leq 2)\) \(f_i=f_{i-1}+f_{i-2}(i>2)\) 一些性质 \(\sum_{i=1}^n f_i=f_{n+2}-1\) \ ...
- windows下PyCharm运行和调试scrapy
Scrapy是爬虫抓取框架,Pycharm是强大的python的IDE,为了方便使用需要在PyCharm对scrapy程序进行调试 python PyCharm Scrapy scrapy指令其实就是 ...
- 《DSP using MATLAB》示例Example7.5
代码: h = [-4, 1, -1, -2, 5, 6, 6, 5, -2, -1, 1, -4]; M = length(h); n = 0:M-1; [Hr, w, b, L] = Hr_Typ ...
- flask第十八篇——模板【2】
请关注公众号:自动化测试实战 上一节我们介绍了模板的基本使用方法,现在我们想一个问题,如果把index.html放到template文件夹下面的文件夹该怎么办呢?其实很容易,当文件夹结构如下图所示时: ...
- CH0805 防线(秦腾与教学评估)
题意 lsp 学习数学竞赛的时候受尽了同仁们的鄙视,终于有一天......受尽屈辱的 lsp 黑化成为了黑暗英雄Lord lsp.就如同中二漫画的情节一样,Lord lsp 打算毁掉这个世界.数学竞赛 ...