Different Types of Dispatch Queues

As mentioned in Short Introduction to Grand Central Dispatch, dispatch queues are pools of threads managed by GCD. We will be working with dispatch queues a lot, so please make sure that you fully understand the concept behind them. There are three types of dispatch queues:

Main Queue

This queue performs all its tasks on the main thread, which is where Cocoa and Cocoa Touch require programmers to call all UI-related methods. Use the dispatch_get_main_queue function to retrieve the handle to the main queue.

Concurrent Queues

These are queues that you can retrieve from GCD in order to execute asynchronous or synchronous tasks. Multiple concurrent queues can be executing multiple tasks in parallel, without breaking a sweat. No more thread management, yippee! Use the dispatch_get_global_queue function to retrieve the handle to a concurrent queue.

Serial Queues

These are queues that, no matter whether you submit synchronous or asynchronous tasks to them, will always execute their tasks in a first-in-first-out (FIFO) fashion, meaning that they can only execute one block object at a time. However, they do not run on the main thread and therefore are perfect for a series of tasks that have to be executed in strict order without blocking the main thread. Use the dispatch_queue_create function to create a serial queue. Once you are done with the queue, you must release it using the dispatch_release function.

At any moment during the lifetime of your application, you can use multiple dispatch queues at the same time. Your system has only one main queue, but you can create as many serial dispatch queues as you want, within reason, of course, for whatever functionality you require for your app. You can also retrieve multiple concurrent queues and dispatch your tasks to them. Tasks can be handed to dispatch queues in two forms: block objects or C functions, as we will see in Dispatching Tasks to Grand Central Dispatch.