Unbounded lock-free queue with many producers and one consumer, without links. More...
#include <grow_mailbox.hpp>
Public Member Functions | |
| TGrowMailbox (size_t initialCapacity=64) | |
| initialCapacity is the size of the first ring, rounded up to a power of two, at least 2. | |
| TGrowMailbox (const TGrowMailbox &)=delete | |
| TGrowMailbox & | operator= (const TGrowMailbox &)=delete |
| void | Push (T &&value) |
| Appends a value. Any thread. | |
| std::optional< T > | Pop () |
| Removes the oldest value, or returns nothing if there is none. Consumer only. | |
| size_t | Capacity () const |
| Capacity of the ring that producers push to now. Any thread; for diagnostics and tests. | |
Unbounded lock-free queue with many producers and one consumer, without links.
Values are moved into the cells of a ring (NDetail::TMpscRing), so a push neither allocates nor links a node. When the open ring is full, the producer that sees it closes it and opens the next ring, twice as large, from a fixed directory of Levels rings. Rings are never freed while the mailbox lives: a slow producer may still hold a pointer to a closed ring, and a level is allocated once, so no reclamation scheme is needed. A mailbox that keeps up stays in one ring.
All rings together are smaller than twice the largest one. While a level opens, every producer that finds it without a ring allocates one, so that none waits for another, and all but one free theirs again: with P producers in Push at once, the memory briefly stays below P + 1 times the new ring.
Push may be called from any thread, Pop from one thread at a time. Values of one producer are popped in the order it pushed them, and a push that happens before another is popped first: the producers go to later rings only, and the consumer leaves a ring only when it is closed and empty.
The mailbox does not wake the consumer. A producer notifies after every push, and an INotifier coalesces the notifications:
Pop may miss a value whose producer has claimed a cell and not written it yet; that producer notifies after it writes.
| T | Value type; its move constructor must not throw. |
| Levels | Size of the directory: the mailbox holds up to initialCapacity * (2^Levels - 1) values. |
|
inline |
Appends a value. Any thread.
| std::bad_alloc | if the next ring cannot be allocated, std::length_error if the directory is exhausted. value is left as it was. |