FOLLY: ProducerConsumerQueue.h














































FOLLY: ProducerConsumerQueue.h



The folly::ProducerConsumerQueue class is a single producer, single consumer queue with very low synchronization overhead.
The queue must be created with a fixed maximum size  and provides just a few simple operations:
• read: An attempt to read the value at the beginning of the queue into a variable, returns false if the queue was empty.
• write: Inserts a value at the end of the queue, returning false if the queue was full.
• frontPtr: Retrieves a pointer to the entry at the beginning of the queue, or nullptr if empty.
• popFront: Remove an item from the front of the queue (the 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 estimate can be slightly incorrect when called by a producer/consumer thread, and very inaccurate if called from any other thread. Therefore, call only from producer/consumer threads!
All these operations are without waiting. Read operations (including frontPtr and popFront) and write operations must only be called by the reader and writer threads. isFull, isEmpty, and sizeGuess can be called by any thread, but return values ​​from read, write, or frontPtr are sufficient for most cases.
writes may fail if the queue is full, and reads may fail if the queue is empty, so in many situations it is important to choose a queue size such that the queue is unlikely to fill up or remain empty for long.

Comments