Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15.

Post on 17-Jan-2018

231 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Methods  add()  removeMin() – removes the top prioritized item.

Transcript

Priority Queues

Dan Dvorin

Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Definition A set of items. Each element has a score (indicating its

priority). Bounded range: the scores are taken from a

discrete set of items. Unbounded range: scores are taken from a

large set.

Methods add() removeMin() – removes the top prioritized

item.

Reminder Linearizability – “requires that each method

call appear to take effect at some instant between its invocation and its response.” (Art of Multiprocessor programming, chapter 15)

Quiescent consistency – “requires that in any execution, when all pending method calls complete, the values they return are consistent with some valid execution of the object.” (Art of Multiprocessor programming, chapter 15)

Array-Based Bounded Priority Queue An array of bins. Bin: a pool of arbitrary items. Supports put(x)

and get(x) which returns an arbitrary item. Items of score i will be in the i-th bin.

…0 1 i r-2 r-1

Picture taken from: http://www.clker.com/cliparts/6/b/5/0/11971056571557644369biswajyotim_Bin.svg.med.png

Tree-Based Bounded Queue m leaves (Bins). m-1internal nodes which are bounded

counters. Counters keep track of the number of items in

the leaves of their left subtree.

Leaf to rootRoot to leaf

Properties The SimpleTree algorithm is not linearizable:

threads may overtake each other. It is quiescently consistent. Lock-free if the counters and bins are lock

free.

Unbounded Heap-Based Queue Each node holds an item and a score. The root holds the minimal scored item, which

is ‘first’ in the queue. A child’s score is no smaller than its parent’s. The (binary) heap is implemented with an

array – children of entry i are entries 2i and 2i+1.

Sift Up

Sift Down

Concurrent Heap - Overview We want to allow concurrent calls to add() and

removeMin(). Sifting up/down steps should be interleaved

between concurrent calls. Linearizable

Concurrent Heap Uses heapLock, to synchronize atomic

modifications to the heap’s fields. HeapNode now has a lock field, a tag field,

and an owner.

Concurrent Heap A node is with a BUSY tag, while it is being

sifted up by an add() call. The owner of a node is decided on its

initialization. add() sifts nodes up, while removeMin() sifts

down. removeMin() is not delayed by an add() call. It

can “snatch” a node underneath an add(). In that case, add() simply searches for its

node up the branch.

Concurrent Heap

Concurrent Heap – removeMin)(

Concurrent Heap – removeMin)(

Concurrent Heap – add)(

•Node is initialized with BUSY tag.•The owner is the calling thread.

Concurrent Heap – add)(

The node was moved up by a removeMin().

Skiplist-Based Unbounded Queue Drawback of the heap – requires constant

rebalancing. We’ll use a skiplist instead. Reminder: a collection of ordered lists. Each

list has a level. Bottom level contains all nodes, each higher level is a sublist of the lower level list.

Skiplist-Based Unbounded Queue Items will be sorted by priority, not by value. Removing an item is done lazily – first the

node is marked as removed, and later is physically removed.

removeMin() scans the bottom level list for the first unmarked node and tries to mark it. On failure, it continues down the list.

On success, skiplist’s remove() is called to remove the node physically.

Skiplist-Based Unbounded Queue

Skiplist-Based Unbounded Queue

Properties Lock free. Quiescently consistent: if an item x was

present before the start of a removeMin() call, then the item returned will have a score less than or equal to that of x.

Not linearizable: a thread might add a higher priority item, and then a lower priority one, and the traversing thread might find and return the later inserted lower priority item (This behavior is quiescently consistent though. It is possible to reorder add() calls concurrent with the removeMin() calls).

top related