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. c# html内容处理类

    using System; using System.Text; using System.Text.RegularExpressions; using System.Net; using Syste ...

  2. 怎么样编译DeepMind?

    可以通过下面的文章来编译著名的deepmind系统. How to build DeepMind LabDeepMind Lab uses Bazel as its build system. Its ...

  3. 正则 匹配 HTML 标签

    var tt=((result.data).toString()).match(/<style(([\s\S])*?)<\/style>/g);

  4. 配置wampserver 虚拟主机

    1.修改http.conf 找到,#Include conf/extra/httpd-vhosts.conf,修改为(有的版本服务器,默认是开启的): 2.配置httpd-vhosts.conf文件, ...

  5. jQuery插件实现表格隔行换色且感应鼠标高亮行变色

    实现表格隔行换色,且感应鼠标行变色的方法有很多,在本文将为大家介绍的是使用jQuery插件来实现,具体如下 看代码: : css代码:  

  6. caffe 学习记录1及网络结构

    ubuntu git clone 默认在当前文件夹 caffe 基础了解:https://www.zhihu.com/question/27982282/answer/39350629 当然,官网才是 ...

  7. 【剑指offer】把二叉树打印成多行,C++实现

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目       从上到下按层打印二叉树,同一层结点从左至右输出,每一层输出一行.例如:下面二叉树的 ...

  8. 机器学习中的ground truth

    ground truth就是参考标准,一般用来做误差量化.比方说要根据历史数据预测某一时间的温度,ground truth就是那个时间的真实温度.error就是(predicted temperatu ...

  9. BZOJ5194: [Usaco2018 Feb]Snow Boots(排序&set)(可线段树优化)

    5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 102  Solved: 79[Subm ...

  10. 数位DP新识

    简单题:HDU2089    HDU3652  HDU4734   HDU3555  POJ3252  HigoCoder1033(需要前导0,或者用方法4) 总结: 1,dfs(pos,state, ...