class to create a lock-free bounded work-stealing queue with priority support
More...
|
| | BoundedPriorityWSQ (PriorityFn fn={}) |
| | constructs the queue with an optional priority function
|
| |
| template<typename O> |
| bool | try_push (O &&item) |
| | tries to insert an item into the sub-queue determined by its priority
|
| |
| template<typename I> |
| size_t | try_bulk_push (I &first, size_t N) |
| | tries to insert a batch of items, routing contiguous same-priority runs to the corresponding sub-queue via bulk push
|
| |
| value_type | pop () |
| | pops an item from the highest-priority non-empty sub-queue
|
| |
| value_type | steal () |
| | steals an item from the highest-priority non-empty sub-queue
|
| |
| bool | empty () const noexcept |
| | queries whether all sub-queues are empty at the time of this call
|
| |
| size_t | size () const noexcept |
| | queries the total number of items across all sub-queues at the time of this call
|
| |
| constexpr size_t | capacity () const |
| | queries the total capacity across all sub-queues
|
| |
| Q & | operator[] (size_t priority) |
| | returns a reference to the sub-queue at the given priority level
|
| |
| const Q & | operator[] (size_t priority) const |
| | returns a const reference to the sub-queue at the given priority level
|
| |
template<BoundedWSQLike Q, size_t MaxPriority, typename PriorityFn = DefaultPriorityFn>
class tf::BoundedPriorityWSQ< Q, MaxPriority, PriorityFn >
class to create a lock-free bounded work-stealing queue with priority support
- Template Parameters
-
| Q | underlying bounded work-stealing queue type; must satisfy BoundedWSQLike |
| MaxPriority | number of priority levels (sub-queues); must be at least 1 |
| PriorityFn | callable (const element&) -> size_t that maps an item to its priority index; the returned value must be in [0, MaxPriority). Defaults to DefaultPriorityFn. |
BoundedPriorityWSQ wraps an array of MaxPriority sub-queues of type Q and routes each item to the sub-queue that corresponds to its priority. Priority levels are numbered from 0 to MaxPriority-1 in decreasing order of urgency: priority 0 is the highest and priority MaxPriority-1 is the lowest. Pop and steal operations always service sub-queues in ascending index order, so higher-priority items are consumed before lower-priority ones.
auto priority_fn = [](tf::Node* n) -> size_t {
return n->priority();
};
tf::Node* n = ...;
pq.try_push(n);
auto item = pq.pop();
class to create a lock-free bounded work-stealing queue with priority support
Definition wsq.hpp:983
template<BoundedWSQLike Q, size_t MaxPriority, typename PriorityFn = DefaultPriorityFn>
the return type of pop and steal operations
Inherits the value_type of the underlying queue Q. For pointer element types, it is the pointer type itself (using nullptr as the empty sentinel); for non-pointer types it is std::optional<T>.
template<BoundedWSQLike Q, size_t MaxPriority, typename PriorityFn = DefaultPriorityFn>
pops an item from the highest-priority non-empty sub-queue
- Returns
- the popped item, or
empty_value() if all sub-queues are empty
Sub-queues are checked in ascending index order (index 0 is highest priority). The element is removed from the owner (back) end of the selected sub-queue, following last-in-first-out (LIFO) order within each priority level.
}
static constexpr auto empty_value()
returns the empty sentinel value for the queue element type
Definition wsq.hpp:751
Only the owner thread may call this method.
template<BoundedWSQLike Q, size_t MaxPriority, typename PriorityFn = DefaultPriorityFn>
steals an item from the highest-priority non-empty sub-queue
- Returns
- the stolen item, or
empty_value() if all sub-queues are empty
Sub-queues are checked in ascending index order (index 0 is highest priority). The element is removed from the thief (front) end of the selected sub-queue, following first-in-first-out (FIFO) order within each priority level.
}
value_type steal()
steals an item from the highest-priority non-empty sub-queue
Definition wsq.hpp:1129
Any thread may call this method concurrently.
template<BoundedWSQLike Q, size_t MaxPriority, typename PriorityFn = DefaultPriorityFn>
template<typename I>
tries to insert a batch of items, routing contiguous same-priority runs to the corresponding sub-queue via bulk push
- Template Parameters
-
| I | random-access iterator type |
- Parameters
-
| first | iterator to the first item; advanced in place by the number of items inserted |
| N | number of items to insert starting at first |
- Returns
- number of items successfully inserted
Scans first for contiguous runs of items that map to the same sub-queue (as determined by _priority_fn) and delegates each run to the underlying queue's try_bulk_push (a single atomic bottom update per run). Stops immediately if a run is only partially inserted (the target sub-queue became full), leaving first pointing at the first uninserted item.
auto fn = [](tf::Node* n) -> size_t { return n->priority(); };
std::vector<tf::Node*> batch = ...;
auto it = batch.begin();
size_t inserted = pq.try_bulk_push(it, batch.size());
Only the owner thread may call this method.