Grand Central Dispatch Bonus Lecture 8

These slides are licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. Grand Central Dispatch

•Parallel processing framework available since OS X Snow Leopard and iOS 4 •Task-based, dispatch queue-based paradigm •Synchronous or asynchronous execution •Abstracts away the underlying threads •Declared in GCD Tasks

•GCD programs divide their work into tasks •Perform some computation •Modify some data structure •Process data from file •etc. •The code for each task is placed inside either a function or a block Dispatch Queues

•GCD manages a number of dispatch queues which manage tasks •All dispatch queues are FIFO •Dispatch queues come in three flavors: •Serial •Concurrent •The main dispatch queue Serial Queues

•Execute tasks one at a time, in order •The currently running task runs on its own thread, managed by the queue •Useful for synchronizing access to a resource Serial Queues

•Any number of serial queues can be created •Each queue operates concurrently with respect to other queues Concurrent Queues

•Execute one or more tasks concurrently •Tasks are started in FIFO order •Each task runs on a distinct thread Concurrent Queues

•Concurrent queues cannot be created by the programmer •Instead, GCD provides three global concurrent queues Main Dispatch Queue

•Globally available serial queue •Executes on the main thread •Works with the run loop to interleave queued tasks with other events (user input, e.g.) •Typically used as synchronization point for the whole application Dispatch Queues

•With a dispatch queue model, the programmer no longer is concerned with thread creation •Instead, can focus on the tasks that need to be completed •Behavior more predictable with dispatch queues What the heck is a block? Blocks

•Basically, a block is a closure •An object-like data structure, created and managed by the compiler •Lives on the heap •Can use variables outside their lexical scope An Example Block

int x = 123; int y = 456;

// Block declaration and assignment void (^aBlock)(int) = ^(int z) { printf("%d %d %d\n", x, y, z); };

// Execute the block aBlock(789); // prints: 123 456 789 Creating Dispatch Queues Concurrent Queues

•Since the programmer can't create concurrent queues, he just needs to ask the system for one of them •GCD provides three concurrent queues, differentiated by priority level (High, low, default) dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); Serial Queues

•Serial queues must be explicitly created •Each serial queue should have some specific purpose - such as protecting a resource dispatch_queue_t queue; queue = dispatch_queue_create("com.example.MyQueue", NULL); Adding Tasks to a Queue Adding Tasks to a Queue

•Tasks may be added to a queue synchronously or asynchronously •Tasks can be added using: • dispatch_async • dispatch_async_f • dispatch_sync • dispatch_sync_f Example

dispatch_queue_t myCustomQueue; myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);

dispatch_async(myCustomQueue, ^{ printf("Do some work here.\n"); }); printf("The first block may or may not have run.\n"); dispatch_sync(myCustomQueue, ^{ printf("Do some more work here.\n"); }); printf("Both blocks have completed.\n"); Map?

for (i = 0; i < count; i++) { printf("%u\n", i); } dispatch_apply

dispatch_queue_t queue; queue = dispatch_get_global_queue( !!!!!!DISPATCH_QUEUE_PRIORITY_DEFAULT, !!!!!!0); dispatch_apply(count, queue, ^(size_t i) { printf("%u\n",i); }); Resources

•Grand Central Dispatch function reference http://developer.apple.com/library/ios/#documentation/Performance/ Reference/GCD_libdispatch_Ref/Reference/reference.html •Concurrency Programming Guide http://developer.apple.com/library/ios/#documentation/General/Conceptual/ ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#// apple_ref/doc/uid/TP40008091-CH102-SW1 •Blocks Programming Topics • http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/ Blocks/Articles/00_Introduction.html