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, returns false iff queue was empty.
  • write: Emplace a value at the end of the queue, returns false iff the queue was full.
  • frontPtr: Retrieve a pointer to the item at the front of the queue, or nullptr 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. isFullisEmpty, and sizeGuess may be called by either thread, but the return values from readwrite, 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的更多相关文章

  1. C#中的线程(二) 线程同步基础

    1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具:                       简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...

  2. 细说.NET中的多线程 (五 使用信号量进行同步)

    上一节主要介绍了使用锁进行同步,本节主要介绍使用信号量进行同步 使用EventWaitHandle信号量进行同步 EventWaitHandle主要用于实现信号灯机制.信号灯主要用于通知等待的线程.主 ...

  3. C#中的多线程 - 同步基础

    原文:http://www.albahari.com/threading/part2.aspx 文章来源:http://blog.gkarch.com/threading/part2.html 1同步 ...

  4. 使用事件等待句柄EventWaitHandler 实现生产者、消费者队列

    using System; using System.Threading; using System.Collections.Generic; class ProducerConsumerQueue ...

  5. C#学习笔记之线程 - 通知Signal

    通知事件等待句柄 Signal With EventWaitHandle 事件等待句柄常用于通知.当一个线程等待直到接收到另外一个线程发出的信号.事件等待句柄是最简单的信号结构,它与C#事件无关.有三 ...

  6. C#中的线程(中)-线程同步

    1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具:                       简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...

  7. C# 多线程学习笔记 - 2

    本文主要针对 GKarch 相关文章留作笔记,仅在原文基础上记录了自己的理解与摘抄部分片段. 遵循原作者的 CC 3.0 协议. 如果想要了解更加详细的文章信息内容,请访问下列地址进行学习. 原文章地 ...

  8. trinitycore 魔兽服务器源码分析(三) 多线程相关

    先看LockedQueue.h template <class T, typename StorageType = std::deque<T> >class LockedQue ...

  9. Java 多线程学习笔记:生产者消费者问题

    前言:最近在学习Java多线程,看到ImportNew网上有网友翻译的一篇文章<阻塞队列实现生产者消费者模式>.在文中,使用的是Java的concurrent包中的阻塞队列来实现.在看完后 ...

随机推荐

  1. (转)MapReduce Design Patterns(chapter 3 (part 1))(五)

    Chapter 3. Filtering Patterns 本章的模式有一个共同点:不会改变原来的记录.这种模式是找到一个数据的子集,或者更小,例如取前十条,或者很大,例如结果去重.这种过滤器模式跟前 ...

  2. Android 和 Dagger 2 中的依赖注入

    原文:Dependency Injection in Android with Dagger 2 作者:Joe Howard 译者:kmyhy 在现代开发团队中到处充斥着"你一定要用依赖注入 ...

  3. 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现

    原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...

  4. my_itoa

    #include <iostream> using namespace std; char *my_reverse(char* s) { char *p,*q; p=s;q=s; whil ...

  5. 今天在win7下安装Fedora22

    <h4>技嘉主板970如何设置BIOS从U盘启动安装系统</h4>1.电脑开机按“Del”键进入主板设置项,选择“BIOS”设置 <a href="http:/ ...

  6. BZOJ2653 middle 【主席树】【二分】*

    BZOJ2653 middle Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样 ...

  7. BZOJ1597土地购买 【斜率优化DP】

    BZOJ1597土地购买 [斜率优化DP] Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足( ...

  8. Spark执行样例报警告:WARN scheduler.TaskSchedulerImpl: Initial job has not accepted any resources

    搭建Spark环境后,调测Spark样例时,出现下面的错误:WARN scheduler.TaskSchedulerImpl: Initial job has not accepted any res ...

  9. altium常用快捷键记录

    选中一个网络的点和线ctrl+h: 翻转器件的层 鼠标拖动+L: 镜像器件 鼠标拖动+x: 查看单一层shift+s: 隐藏/查看某些器件ctrl+d:

  10. HAL层编写规范

    andriod HAL模块也有一个通用的入口地址,这个入口地址就是HAL_MODULE_INFO_SYM变量,通过它,我们可以访问到HAL模块中的所有想要外部访问到的方法.  在Linux系统中,后缀 ...