Home Grand Central Dispatch (GCD)
Post
Cancel

Grand Central Dispatch (GCD)

Grand Central Dispatch:

 

+ Dispatch Queue: It’s an object which is a queue, and that queue contains n number of functions or blocks as their tasks. Library automatically creates different priority levels to execute these tasks.

 

  • It’s a C API, but not standard C API, Its apple’s mechanism.
  • It’s a core multithreading API developed by Apple developers.
  • It optimizes application support for devices with multicore processors.
  • It’s an implementation of Task Parallelism based on the Thread Pool Pattern.
  • It was released with OS X 10.6 and iOS 4.0 and above.
  • library name:- libdispatch.dylib

 

  • GCD provides and manages FIFO queues.
  • GCD queue is a set of Block objects.
  • Blocks are executed on a pool of threads fully managed by the system.
  • No guarantee is made on a task execution.

 

Functions by Task

  1. dispatch_get_global_queue - Returns a well-known global CONCURRENT queue of a given priority level.
  2. dispatch_get_main_queue - Returns the SERIAL dispatch queue associated with the application’s MAIN THREAD.
  3. dispatch_queue_create - Creates a new dispatch queue to which blocks can be submitted.
  4. dispatch_get_current_queue - Returns the queue on which the currently executing block is running.
  5. dispatch_queue_get_label - Returns the label specified for the queue when the queue was created.
  6. dispatch_set_target_queue - Sets the target queue for the given object.
  7. dispatch_main - Executes blocks submitted to the main queue.

 

  1. dispatch_async - Submits a block for asynchronous execution on a dispatch queue and returns immediately.
  2. dispatch_async_f - Submits an application-defined function for asynchronous execution on a dispatch queue and returns immediately.
  3. dispatch_sync - Submits a block object for execution on a dispatch queue and waits until that block completes.
  4. dispatch_sync_f - Submits an application-defined function for synchronous execution on a dispatch queue.
  5. dispatch_after - Enqueue a block for execution at the specified time.
  6. dispatch_after_f - Enqueues an application-defined function for execution at a specified time.
  7. dispatch_apply - Submits a block to a dispatch queue for multiple invocations.
  8. dispatch_apply_f - Submits an application-defined function to a dispatch queue for multiple invocations.
  9. dispatch_once - Executes a block object once and only once for the lifetime of an application.

 

GCD offers three kinds of queues:

  • Main:- Tasks execute serially on your application’s main thread. (The main queue is automatically created by the system and associated with your application’s main thread.)
  • Concurrent:- Tasks are dequeued in FIFO order, but run concurrently and can finish in any order.
  • Serial - Serial Queue can only run one task at a time in FIFO order. A client to library may also create any number of serial queues, which execute tasks in the order they are submitted.

 

Managing Dispatch Objects - Dispatch objects must be manually retained and released and are not garbage collected.

  1. dispatch_debug - Programmatically logs debug information about a dispatch object.
  2. dispatch_get_context - Returns the application-defined context of an object.
  3. dispatch_release - Decrements the reference (retain) count of a dispatch object.
  4. dispatch_resume - Resume the invocation of block objects on a dispatch object.
  5. dispatch_retain - Increments the reference (retain) count of a dispatch object.
  6. dispatch_set_context - Associates an application-defined context with the object.
  7. dispatch_set_finalizer_f - Sets the finalizer function for a dispatch object.
  8. dispatch_suspend - Suspends the invocation of block objects on a dispatch object.

 

Other  - TODO

  • Dispatch Semaphores - Executes a certain number of blocks/functions concurrently.
  • Dispatch Barriers
  • Dispatch Sources - Executes blocks or functions asynchronously.
  • Dispatch I/O Convenience API
  • Dispatch I/O Channel API
  • Dispatch Data Objects
  • Managing Time and
  • Dispatch Queue-Specific Context Data
This post is licensed under CC BY 4.0 by the author.