zoukankan      html  css  js  c++  java
  • 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, 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();
          }
        });
  • 相关阅读:
    十一招解决:系统IE部分网页打不开怎么办(转载)
    基于阿里云server搭建SVNserver
    hdu 2825 Wireless Password(ac自己主动机&amp;dp)
    压力单位MPa、Psi和bar之间换算公式
    hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)
    图像处理之霍夫变换(直线检測算法)
    hdu1171 Big Event in HDU 01-背包
    [视频] x264 压缩笔记
    UVALive 4043 Ants
    关于游戏手柄按键的设计
  • 原文地址:https://www.cnblogs.com/lenmom/p/9359133.html
Copyright © 2011-2022 走看看