Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Blocks andGrand Central DispatchBy Robert Brown@robby_brown
Blocks
BlocksBlocks are a proposed addition to C.Like a function pointer, except it also stores the contextthe block was created ...
BlocksDeclaration Syntax:returnType (^blockName) (Arguments)Blocks may be anonymous.Definition Syntax:^ returnType (Argumen...
BlocksBlocks can modify local variables outside of their scopeif the variables have the new __block keyword.Global and sta...
GCD
What is GCD?GCD is a lightweight multithreading engine.Uses a thread pool.Developers create queues of blocks rather thanth...
Why Multithread on a SingleCore?Keeps the UI responsive.UI code runs on the main thread.Everything else runs on a backgrou...
Frequently Used APIsdispatch_async(queue, block);dispatch_queue_create(name, NULL);dispatch_set_target_queue(object, queue...
Global QueuesFour global queues:  Main, Low, Default, and High.Only the main thread services the main queue.The three othe...
Advanced GCD
User Queues                                     Jedi                                                LevelDevelopers can cr...
User Queues                                       Jedi                                                  LevelAll queues mu...
User Queues                                      Jedi                                                 Level        Default...
Dispatch Sources                                  Jedi                                                  LevelQueues can re...
Semaphores                                          Sith                                                   LevelSemaphores...
Demo
Want to Learn More?Apple DocumentationWWDC 2010 Session 206 Session 211WWDC 2008 Session 382
Questions?
Upcoming SlideShare
Loading in …5
×

Grand Central Dispatch

1,989 views

Published on

Grand Central Dispatch (GCD) and blocks are a great tool that makes multi-threading easy, efficient, and enjoyable.

Published in: Technology
  • Be the first to comment

Grand Central Dispatch

  1. 1. Blocks andGrand Central DispatchBy Robert Brown@robby_brown
  2. 2. Blocks
  3. 3. BlocksBlocks are a proposed addition to C.Like a function pointer, except it also stores the contextthe block was created in.Similar to closures, lambdas, and anonymous functionsin other languages.
  4. 4. BlocksDeclaration Syntax:returnType (^blockName) (Arguments)Blocks may be anonymous.Definition Syntax:^ returnType (Arguments) { code; }The return type and arguments are optional.GCD provides function pointer variants to the block APIs.
  5. 5. BlocksBlocks can modify local variables outside of their scopeif the variables have the new __block keyword.Global and static variables can be modified without the__block keyword.Blocks automatically retain Objective-C objects.C objects must be manually retained.
  6. 6. GCD
  7. 7. What is GCD?GCD is a lightweight multithreading engine.Uses a thread pool.Developers create queues of blocks rather thanthreads.Uses lock-less exclusion rather than mutual exclusion.Replaces blocking and polling APIs.
  8. 8. Why Multithread on a SingleCore?Keeps the UI responsive.UI code runs on the main thread.Everything else runs on a background thread.Prevents the main thread from blocking or waiting.
  9. 9. Frequently Used APIsdispatch_async(queue, block);dispatch_queue_create(name, NULL);dispatch_set_target_queue(object, queue);dispatch_get_global_queue(priority, flags);dispatch_retain(object);dispatch_release(object);
  10. 10. Global QueuesFour global queues: Main, Low, Default, and High.Only the main thread services the main queue.The three other queues determine the priority ofbackground tasks.Enqueuing is thread safe.
  11. 11. Advanced GCD
  12. 12. User Queues Jedi LevelDevelopers can create their own queues.Create a queue for each task, subsystem, or resource.Queues should use reverse-DNS naming. Example: edu.byu.cocoaheads.networkqueue
  13. 13. User Queues Jedi LevelAll queues must eventually drain to a global queue.Queues can be organized into arbitrary hierarchies.Don’t create loops!
  14. 14. User Queues Jedi Level Default Priority Default ≠ Queue 1 Queue Queue Queue 1 2 3Queue Queue 2 3
  15. 15. Dispatch Sources Jedi LevelQueues can respond to low-level events.Decouples monitoring and event handling.Avoids polling for an event. Example: Polling for a directory change or socket available.Dispatch sources can be suspended, resumed, andcancelled.
  16. 16. Semaphores Sith LevelSemaphores are used to create locks.Available if you need them.Usually block the running thread.The availability of the semaphore can be checkedwithout blocking (i.e. Test and Set).
  17. 17. Demo
  18. 18. Want to Learn More?Apple DocumentationWWDC 2010 Session 206 Session 211WWDC 2008 Session 382
  19. 19. Questions?

×