Chapter 7

GCD Basics

In this chapter, we show how to use GCD. At first, we start with a dispatch queue that is a core concept of GCD and show how to add tasks to the queues. After that, we design an API to control queues and tasks. Finally, we also show how to dispatch I/0 to control files using GCD.

Dispatch Queues

A dispatch queue is, as its name suggests, a queue to store tasks to be executed. Application programmers can write tasks as Block literals and add them to a dispatch queue by a dispatch_async function, and the like. Then the dispatch queue executes the tasks in the added order, first-in-first-out (FIFO), as shown in Figure 7–1.

Figure 7–1. Execution on a dispatch queue

As shown in Figure 7–2, there are two kinds of dispatch ...

Get Pro Multithreading and Memory Management for iOS and OS X now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.