120#ifndef __ADS_MultiQueue
121 #define __ADS_MultiQueue
139#include <type_traits>
154template<
class E ,
class C >
159template<
class K ,
class T ,
class Less = std::less< K > ,
164 static_assert( std::numeric_limits< K >::is_specialized ,
165 "the keys are integers or reals" );
166 static_assert( std::atomic< K >::is_always_lock_free ,
167 "the keys fit an atomic word without a lock" );
192 std::uint64_t seed = 1 ,
193 Less less = Less() );
207 [[nodiscard]] std::size_t
queues(
void )
const {
return( f_n ); }
210 [[nodiscard]]
unsigned stickiness(
void )
const {
return( f_s ); }
214 [[nodiscard]] std::size_t
size(
void )
const;
217 [[nodiscard]]
bool empty(
void )
const {
return(
size() == 0 ); }
226 static constexpr std::size_t kBuf = 16;
232 bool operator()(
const Entry & a ,
const Entry & b )
const {
233 return( less( a.
key , b.
key ) );
239 struct alignas( 64 ) Queue
242 std::atomic< bool > lock{
false };
244 std::uint8_t nd = 0 , ni = 0;
246 std::atomic< K > top;
254 H< Entry , ByKey > heap;
256 explicit Queue(
const ByKey & less , K none ) : top(
none ) ,
262 [[nodiscard]]
static K none(
const Less & less );
265 [[nodiscard]]
static bool tryLock( Queue & q );
268 static void lock( Queue & q );
271 static void unlock( Queue & q );
274 void put( Queue & q ,
Entry && e )
const;
277 void flush( Queue & q )
const;
280 Entry take( Queue & q )
const;
283 void publish( Queue & q )
const;
286 [[nodiscard]]
bool ordered(
const Queue & q )
const;
290 std::optional< Entry > scan( std::size_t r );
297 std::atomic< std::uint64_t > f_seed;
307template<
class K ,
class T ,
class Less ,
template<
class ,
class >
class H >
406 Handle(
MultiQueue & m , std::uint64_t seed ) : f_m( &m ) , f_x( seed ) { }
409 std::uint64_t next(
void );
412 std::size_t bit(
void );
415 std::size_t any(
void );
425 std::size_t f_k[ 2 ] = { 0 , 0 };
426 unsigned f_used = ~0u;
427 std::uint64_t f_bits = 0;
440#include "ADS/concurrent/impl/MultiQueue.tpp"
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A -ary heap of plain items, beyond the book: the heap of section 10.1 with children per node in a ve...
a -ary heap of plain items, the best one by better() on top; pop() by the walk down from the top,...
Definition DaryHeap.h:128
the access of one thread to a MultiQueue: its draws and the two queues it keeps
Definition MultiQueue.h:309
std::optional< Entry > tryPop(void)
the least pair of the better of the two queues kept, or of the first draw of two queues whose better ...
void pushAll(It first, It last)
puts the pairs of , which convert to Entry, all into one of the two queues kept, under one lock,...
void push(K k, T x)
puts the pair into one of the two queues kept, or into the first free one of other draws; the key by...
friend class MultiQueue
Definition MultiQueue.h:403
MultiQueue(std::size_t n, unsigned s=16, std::uint64_t seed=1, Less less=Less())
the structure of empty queues, kept by a thread for operations, with the draws from the seed seed; ...
std::size_t size(void) const
the number of the pairs, counted in each queue under its lock, exact when no operation runs
MultiQueue & operator=(const MultiQueue &)=delete
no copy, since the threads share it by its address
MultiQueue(const MultiQueue &)=delete
no copy, since the threads share it by its address
K Key
the type of the keys
Definition MultiQueue.h:171
~MultiQueue()
the frees of the queues
static constexpr decrease_key_kind decrease_key
a relaxed queue with no handles, on which a search lowers a key by a new insertion [see decrease_key_...
Definition MultiQueue.h:176
unsigned stickiness(void) const
the operations a thread keeps its two queues for
Definition MultiQueue.h:210
bool empty(void) const
whether size() is 0
Definition MultiQueue.h:217
bool consistent(void) const
the buffers of each queue ordered and holding its least pairs, and the atomic minima equal to them; u...
std::size_t queues(void) const
the number of the queues
Definition MultiQueue.h:207
T Item
the type of the items
Definition MultiQueue.h:172
Handle handle(void)
a handle for one thread, with a generator of its own
decrease_key_kind
how a priority queue lowers the priority of an item: it cannot (none), by a new insertion and the sta...
Definition Capabilities.h:553
@ lazy
Definition Capabilities.h:553
@ none
Definition Capabilities.h:553
DaryHeap< E, C, 8 > MultiQueueHeap
the inner queue of a MultiQueue by default, the 8-ary heap
Definition MultiQueue.h:155
a pair of the queue, its key and its item
Definition MultiQueue.h:180
T item
the item
Definition MultiQueue.h:182
K key
the key, the least popped first
Definition MultiQueue.h:181