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
- dispatch_get_global_queue - Returns a well-known global CONCURRENT queue of a given priority level.
- dispatch_get_main_queue - Returns the SERIAL dispatch queue associated with the application’s MAIN THREAD.
- dispatch_queue_create - Creates a new dispatch queue to which blocks can be submitted.
- dispatch_get_current_queue - Returns the queue on which the currently executing block is running.
- dispatch_queue_get_label - Returns the label specified for the queue when the queue was created.
- dispatch_set_target_queue - Sets the target queue for the given object.
- dispatch_main - Executes blocks submitted to the main queue.
- dispatch_async - Submits a block for asynchronous execution on a dispatch queue and returns immediately.
- dispatch_async_f - Submits an application-defined function for asynchronous execution on a dispatch queue and returns immediately.
- dispatch_sync - Submits a block object for execution on a dispatch queue and waits until that block completes.
- dispatch_sync_f - Submits an application-defined function for synchronous execution on a dispatch queue.
- dispatch_after - Enqueue a block for execution at the specified time.
- dispatch_after_f - Enqueues an application-defined function for execution at a specified time.
- dispatch_apply - Submits a block to a dispatch queue for multiple invocations.
- dispatch_apply_f - Submits an application-defined function to a dispatch queue for multiple invocations.
- 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.
- dispatch_debug - Programmatically logs debug information about a dispatch object.
- dispatch_get_context - Returns the application-defined context of an object.
- dispatch_release - Decrements the reference (retain) count of a dispatch object.
- dispatch_resume - Resume the invocation of block objects on a dispatch object.
- dispatch_retain - Increments the reference (retain) count of a dispatch object.
- dispatch_set_context - Associates an application-defined context with the object.
- dispatch_set_finalizer_f - Sets the finalizer function for a dispatch object.
- 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