Grand Central Dispatch

1,832 views

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,832
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
41
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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?

    ×