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

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

Detailed description

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.

See also
L. Lamport, Specifying concurrent program modules, ACM Transactions on Programming Languages and Systems 5(2), 190-222, 1983, doi:10.1145/69624.357207
P. P. C. Lee, T. Bu, G. Chandranmenon, A lock-free, cache-efficient multi-core synchronization mechanism for line-rate network traffic monitoring, IEEE International Symposium on Parallel and Distributed Processing (IPDPS), 1-12, 2010, doi:10.1109/IPDPS.2010.5470368 (the copies of the indices)
J. Wang, K. Zhang, X. Tang, B. Hua, B-Queue: efficient and practical queuing for fast core-to-core communication, International Journal of Parallel Programming 41(1), 137-159, 2013, doi:10.1007/s10766-012-0213-x (the wait for a batch)
Intel 64 and IA-32 Architectures Optimization Reference Manual, the spatial prefetcher that fetches the pairs of lines of 128 bytes, https://www.intel.com/content/www/us/en/content-details/ 671488/intel-64-and-ia-32-architectures-optimization-reference- manual-volume-1.html
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa