Friday, April 08, 2011

WWDC2010 Session211 Simplifying iPhone App Development with GCD

GCD Overview

1. GCD is part of libSystem.dylib

2. Available to all Apps.

- #include <dispatch/dispatch.h>

3. GCD API has block-based and function-based variants

- Focus today on block-based API

Introduction to GCD recap

1. Blocks

- dispatch_async()

2. Queues

- Lightweight list of blocks

- Enqueue/dequeue is FIFO

3. dispatch_get_main_queue()

- Main thread/main runloop

4. dispatch_queue_create()

- Automatic helper thread

GCD Advantages

1. Efficiency - More CPU cycles available for your code

2. Better metaphors

- Blocks are easy to use

- Queues are inherently producer/consumer

3. Systemwide perspective

- Only the OS can balance unrelated subsystems


1. Existing threading and synchronization primitives are 100% compatible

2. GCD threads are wrapped POSIX threads

- Do not cancel, exit, kill, join, or detach GCD threads

3. GCD reuses threads

- Restore any per-thread state changed within a block


Covered by Session 206


1. Enforce mutually exclusive access to critical sections

2. Serialize access to shared state between threads

3. Ensure data integrity

Without GCD

- (void)updateImageCacheWithImg:(UIImage*)img {

NSLock *l = self.imageCacheLock;

[l lock];

// Critical section

if ([self.imageCache containsObj:img]) {

[l unlock]; // Don't forget to unlock



[self.imageCache addObj:img];

[l unlock];


With GCD

- (void)updateImageCacheWithImg:(NSImage*)img {

dispatch_queue_t queue = self.imageCacheQueue;

dispatch_sync(queue, ^{ //You can change dispatch_sync to dispatch_async to defer critical section

// Critical section

if ([self.imageCache containsObj:img]) {



[self.imageCache addObj:img];



Inter-Thread Communication

Performing selectors (Without GCD)

– performSelectorOnMainThread:withObject:waitUntilDone:

– performSelector:onThread:withObject:waitUntilDone:

– performSelector:withObject:afterDelay:

– performSelectorInBackground:withObject:

GCD code equal to performSelector:onThread:withObject:waitUntilDone:

// waitUntilDone: NO

dispatch_async(queue, ^{

[myObject doSomething:foo withData:bar];


// waitUntilDone: YES

dispatch_sync(queue, ^{

[myObject doSomething:foo withData:bar];


GCD code equal to performSelector:withObject:afterDelay:dispatch_time_t delay;delay = dispatch_time(DISPATCH_TIME_NOW, 50000 /* 50μs */);dispatch_after(delay, queue, ^{ [myObject doSomething:foo withData:bar];});GCD code equal to performSelectorInBackground:withObject:dispatch_queue_t queue = dispatch_get_global_queue(0, 0);dispatch_async(queue, ^{ [myObject doSomething:foo withData:bar];});
Global Queues1. Enqueue/dequeue is FIFOGCD queue 1

2. Concurrent execution- Non-FIFO completion orderGCD queue 2 3. dispatch_get_global_queue(priority, 0)

4. Global queues map GCD activity to real threads
Lots of contents in the following 3 sections beyond my understanding. Go watching the video if interested.- Dispatch Sources
- Source Cancellation- Target Queues
GCD Objects

GCD functions

GCD functions

Managing object lifetime

1. Ensure objects captured by blocks are valid when blocks are executed

- Objects must be retained and released around asynchronous operations

2. Objective-C objects captured by blocks are auto-retained and auto-released

3. Other objects captured by blocks must be retained by your code

- CFRetain()/CFRelease()

- dispatch_retain()/dispatch_release()

Suspend and resume

1. Suspend and resume only affects queues and sources you create

- Sources are created suspended

2. Suspension is asynchronous

- Takes effect between blocks

3. Your queues can predictably suspend objects that target them

Application contexts

1. Applications can attach custom data to GCD objects

- dispatch_set_context()/dispatch_get_context()

2. Optional finalizer callback

- dispatch_set_finalizer_f()

- Allows attached context to be freed with object

- Called on the target queue of the object

WWDC2010 Session211 Simplifying iPhone App Development with Grand Central Dispatch

Daniel Steffen - Core OS

No comments: