|
ADS
Algorithms and Data Structures
|
A bounded queue between one producer thread and one consumer thread, lock-free and wait-free, beyond the book: the circular vector of the queue of section 4.3.3 [see ArrayQueue.h], whose head only the consumer writes and whose tail only the producer writes, so that each index is an atomic with the release and acquire orders and no lock is needed (the queue of Lamport). More...
#include "ADS/Contract.h"#include "ADS/concurrent/EventCount.h"#include "ADS/memory/Prefetch.h"#include <algorithm>#include <atomic>#include <bit>#include <cstddef>#include <memory>#include <new>#include <optional>#include <utility>#include "ADS/concurrent/impl/SPSCQueue.tpp"Go to the source code of this file.
Classes | |
| class | ADS::SPSCQueue< T > |
| a bounded queue from one producer to one consumer, lock-free More... | |
Namespaces | |
| namespace | ADS |
A bounded queue between one producer thread and one consumer thread, lock-free and wait-free, beyond the book: the circular vector of the queue of section 4.3.3 [see ArrayQueue.h], whose head only the consumer writes and whose tail only the producer writes, so that each index is an atomic with the release and acquire orders and no lock is needed (the queue of Lamport).
Each thread keeps a copy of the index of the other one, read again only when the queue looks full or empty, and the two indices and the two copies sit on four lines of cache of their own, grouped in pairs of lines so that a thread that spins on a full or empty queue, rewriting its copy, does not invalidate the line that the other one writes or reads (false sharing, also through the prefetch of the adjacent line). A consumer that finds the queue empty asks for the line of its next place before it reads the tail again, so that, when the item has come, the two lines travel together and a round trip between the threads costs one miss a side instead of two in a row; a consumer whose last pop took an item and that finds fewer than \( \min( 64 , c / 2 ) \) new ones, \( c \) the capacity, waits 64 pauses of the processor for a batch before it takes them (the backtracking of B-Queue), while after a pop that found nothing it takes at once what comes, so that a single item after a silence costs no wait. The places are raw memory with a line of padding at each end, so that the first and the last items share no line with other data. The capacity is a power of 2 and the indices, which only grow, are reduced by a mask.
The contracts [see Contract.h]: the capacity is a power of 2, at least 2; only one thread calls push() and only one calls pop(). The operations are defined in SPSCQueue.tpp.