6dc0344b204ff10941136c61b1cdd7df8cad255d
[folly.git] / folly / docs / ProducerConsumerQueue.md
1 `folly/ProducerConsumerQueue.h`
2 -------------------------------
3
4 The `folly::ProducerConsumerQueue` class is a one-producer
5 one-consumer queue with very low synchronization overhead.
6
7 The queue must be created with a fixed maximum size (and allocates
8 that many cells of sizeof(T)), and it provides just three simple
9 operations: read, write, and isFull.  All of these operations are
10 wait-free.  The read and write operations must only be called by the
11 reader and writer thread, respectively, but isFull is accessible to
12 both.
13
14 Both read and write may fail if the queue is full, so in many
15 situations it is important to choose the queue size such that the
16 queue filling up for long is unlikely.
17
18 ### Example
19 ***
20
21 A toy example that doesn't really do anything useful:
22
23 ``` Cpp
24     folly::ProducerConsumerQueue<folly::fbstring> queue;
25
26     std::thread reader([&queue] {
27       for (;;) {
28         folly::fbstring str;
29         while (!queue.read(str)) continue;
30
31         sink(str);
32       }
33     });
34
35     // producer thread:
36     for (;;) {
37       folly::fbstring str = source();
38       while (!queue.write(str)) continue;
39     }
40 ```