|
ADS
Algorithms and Data Structures
|
A bounded queue of many producers and many consumers, beyond the book: the circular vector of Vyukov, whose places carry a sequence number besides the item. More...
#include "ADS/Contract.h"#include <algorithm>#include <atomic>#include <bit>#include <cstddef>#include <memory>#include <new>#include <utility>#include "ADS/concurrent/impl/MPMCQueue.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MPMCQueue< T > |
| a bounded queue of many producers and many consumers More... | |
Namespaces | |
| namespace | ADS |
A bounded queue of many producers and many consumers, beyond the book: the circular vector of Vyukov, whose places carry a sequence number besides the item.
The place of the index \( i \), \( i \bmod c \) for a capacity \( c \) that is a power of 2, holds the sequence \( i \) when it is free for the push of index \( i \), \( i + 1 \) when it holds the item of index \( i \), and \( i + c \) when that item has been taken and the place waits for the push of index \( i + c \). A producer reads the tail \( p \) and the sequence \( s \) of its place: if \( s = p \) it claims the index by a compare-and-swap of the tail from \( p \) to \( p + 1 \), builds the item and publishes it by the sequence \( p + 1 \) (a release); if \( s < p \) the queue is full; if \( s > p \) another producer took \( p \), and it reads the tail again. A consumer does the same on the head \( h \) with the sequence \( h + 1 \), and frees the place by the sequence \( h + c \). The tail and the head sit on lines of cache of their own, so that producers and consumers meet only at the places they share, one read-modify-write per operation; no item is ever read before its producer published it, nor a place reused before its consumer freed it. As in the queue of Vyukov, a thread that stops between its compare-and-swap and its publication holds up the others when they come round to that place: the queue is lock-free in the common case, not in the strict sense.
The contracts [see Contract.h]: the capacity is a power of 2, at least