ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MPMCQueue.h File Reference

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

Detailed description

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

  1. The operations are defined in MPMCQueue.tpp.
See also
D. Vyukov, Bounded MPMC queue, 2010, https://www.1024cores.net/home/lock-free-algorithms/queues/ bounded-mpmc-queue
R. Nikolaev, A scalable, portable, and memory-efficient lock-free FIFO queue, International Symposium on Distributed Computing (DISC), LIPIcs 146, 28:1-28:16, 2019, doi:10.4230/LIPIcs.DISC.2019.28 (the rings with a cycle per place)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa