4
votes

I think I read once about a way to add a block to the front of a dispatch queue in Grand Central Dispatch (so it doesn't wait on other blocks that are pending). But I can't find it in the GCD Reference. Does anyone know if this is possible, and if so how?

2

2 Answers

4
votes

There is no public API to "jump" a queue, nor to remove or reorder previously-enqueued-but-not-yet-executed blocks from a queue. If you just want a block to execute at a higher priority than others, you can submit it to the high priority global concurrent queue, but naturally any concurrency guarantees being provided by using a specific queue will be lost by doing that. Beyond that, you would have to implement this functionality yourself.

One way to do that might be to have your own wrappers around dispatch_async that would wrap each block in another block that would give "queue jumping" blocks a chance to execute before every non-queue-jumped block. One could imagine making a second serial queue for "jumpers", which these wrapper blocks could suspend and resume accordingly.

But in general, once submitted, there is no way to reorder blocks in a GCD queue.

2
votes

There are techniques using target queues and suspension that can be used to implement something similar with multiple interrelated queues.

The WWDC 2011 session "Mastering Grand Central Dispatch" covered one of these in some detail.