Question

I am trying to understand what priority is going to be used to run dispatch blocks that are dispatched on a custom serial queue declared as:

dispatch_queue_t queue = dispatch_queue_create("com.purposeOfQueue.queue", DISPATCH_QUEUE_SERIAL);

So, here, I am only saying that "queue" is a serial queue. But, what priority is the system going to use for this queue. I know there's HIGH, DEFAULT, LOW, BACKGROUND.

I also know I could do this:

dispatch_set_target_queue(queue, DISPATCH_QUEUE_PRIORITY_DEFAULT);

Which would make it so the queue get a DEFAULT priority.

But if I just do what I showed above?

dispatch_queue_t queue = dispatch_queue_create("com.purposeOfQueue.queue", DISPATCH_QUEUE_SERIAL);

What priority is that going to use?

Was it helpful?

Solution

It's useful to note that from iOS 8 the creation of queues and their abilities have changed. You would use the following to get a serial queue with the lowest priority.

dispatch_queue_attr_t queueAttributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_BACKGROUND, 0);
self.queue = dispatch_queue_create("com.abc.myQueue", queueAttributes);

OTHER TIPS

If you don't explicitly force a priority, it will use the "default" priority. This kinda goes right to the heart of the meaning of the word "default."

I believe that you can accomplish what you're looking for with:

    dispatch_set_target_queue(queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));

or DISPATCH_QUEUE_PRIORITY_LOW as the case may be.

Check the Apple documentation of serial queues.

Serial queues are useful when you want your tasks to execute in a specific order. A serial queue executes only one task at a time and always pulls tasks from the head of the queue. You might use a serial queue instead of a lock to protect a shared resource or mutable data structure. Unlike a lock, a serial queue ensures that tasks are executed in a predictable order. And as long as you submit your tasks to a serial queue asynchronously, the queue can never deadlock.

and from queue.h:

/*!
 * @const DISPATCH_QUEUE_SERIAL
 * @discussion A dispatch queue that invokes blocks serially in FIFO order.
 */
#define DISPATCH_QUEUE_SERIAL NULL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top