Hide deprecated symbols D
Auto-expand all symbols A
On This Page
Language: Swift Objective-C Both
Grand Central Dispatch (GCD) Reference
Important
This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This information is subject to change, and software implemented according to this document should be tested with final operating system software and final documentation. Newer versions of this document may be provided with future betas of the API or technology.
Inheritance
Not Applicable
Conforms To
Not Applicable
Import Statement
Not Applicable
Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and OS X.
The BSD subsystem, CoreFoundation, and Cocoa APIs have all been extended to use these enhancements to help both the system and your application to run faster, more efficiently, and with improved responsiveness. Consider how difficult it is for a single application to use multiple cores effectively, let alone doing it on different computers with different numbers of computing cores or in an environment with multiple applications competing for those cores. GCD, operating at the system level, can better accommodate the needs of all running applications, matching them to the available system resources in a balanced fashion.
This document describes the GCD API, which supports the asynchronous execution of operations at the Unix level of the system. You can use this API to manage interactions with file descriptors, Mach ports, signals, or timers. In OS X v10.7 and later, you can also use GCD to handle general purpose asynchronous I/O operations on file descriptors.
GCD is not restricted to system-level applications, but before you use it for higher-level applications, you should consider whether similar functionality provided in Cocoa (via NSOperation and block objects) would be easier to use or more appropriate for your needs. See Concurrency Programming Guide for more information.
Important
Be careful when mixing GCD with the fork system call. If a process makes GCD calls prior to calling fork, it is not safe to make additional GCD calls in the resulting child process until after a successful call to exec or related functions.
GCD Objects and Automatic Reference Counting
When you build your app using the Objective-C compiler, all dispatch objects are Objective-C objects. As such, when automatic reference counting (ARC) is enabled, dispatch objects are retained and released automatically just like any other Objective-C object. When ARC is not enabled, use the dispatch_retain and dispatch_release functions (or Objective-C semantics) to retain and release your dispatch objects. You cannot use the Core Foundation retain/release functions.
If you need to use retain/release semantics in an ARC-enabled app with a later deployment target (for maintaining compatibility with existing code), you can disable Objective-C-based dispatch objects by adding -DOS_OBJECT_USE_OBJC=0 to your compiler flags.
Functions
Creating and Managing Queues
dispatch_get_main_queue
dispatch_get_global_queue
dispatch_queue_create
dispatch_get_current_queue
(iOS 6.0)
dispatch_queue_attr_make_with_qos_class
dispatch_queue_get_label
dispatch_set_target_queue
dispatch_main
Queuing Tasks for Dispatch
GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Blocks submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes. GCD offers three kinds of queues:
Main: tasks execute serially on your application’s main thread
Concurrent: tasks are dequeued in FIFO order, but run concurrently and can finish in any order.
Serial: tasks execute one at a time in FIFO order
The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:
Calling dispatch_main
Calling UIApplicationMain (iOS) or NSApplicationMain (OS X)
Using a CFRunLoopRef on the main thread
Use concurrent queues to execute large numbers of tasks concurrently. GCD automatically creates four concurrent dispatch queues (three prior to iOS 5 or OS X v10.7) that are global to your application and are differentiated only by their priority level. Your application requests these queues using the dispatch_get_global_queue function. Because these concurrent queues are global to your application, you do not need to retain and release them; retain and release calls for them are ignored. In OS X v10.7 and later or iOS 4.3 and later, you can also create additional concurrent queues for use in your own code modules.
Use serial queues to ensure that tasks to execute in a predictable order. It’s a good practice to identify a specific purpose for each serial queue, such as protecting a resource or synchronizing key processes. Your application must explicitly create and manage serial queues. It can create as many of them as necessary, but should avoid using them instead of concurrent queues just to execute many tasks simultaneously.
Important
GCD is a C level API; it does not catch exceptions generated by higher level languages. Your application must catch all exceptions before returning from a block submitted to a dispatch queue.
dispatch_async
dispatch_async_f
dispatch_sync
dispatch_sync_f
dispatch_after
dispatch_after_f
dispatch_apply
dispatch_apply_f
dispatch_once
dispatch_once_f
Using Dispatch Groups
Grouping blocks allows for aggregate synchronization. Your application can submit multiple blocks and track when they all complete, even though they might run on different queues. This behavior can be helpful when progress can’t be made until all of the specified tasks are complete.
dispatch_group_async
dispatch_group_async_f
dispatch_group_create
dispatch_group_enter
dispatch_group_leave
dispatch_group_notify
dispatch_group_notify_f
dispatch_group_wait
Managing Dispatch Objects
GCD provides dispatch object interfaces to allow your application to manage aspects of processing such as memory management, pausing and resuming execution, defining object context, and logging task data. Dispatch objects must be manually retained and released and are not garbage collected.
dispatch_debug
(iOS 6.0)
dispatch_get_context
dispatch_release
dispatch_resume
dispatch_retain
dispatch_set_context
dispatch_set_finalizer_f
dispatch_suspend
Using Semaphores
A dispatch semaphore is an efficient implementation of a traditional counting semaphore. Dispatch semaphores call down to the kernel only when the calling thread needs to be blocked. If the calling semaphore does not need to block, no kernel call is made.
dispatch_semaphore_create
dispatch_semaphore_signal
dispatch_semaphore_wait
Using Barriers
A dispatch barrier allows you to create a synchronization point within a concurrent dispatch queue. When it encounters a barrier, a concurrent queue delays the execution of the barrier block (or any further blocks) until all blocks submitted before the barrier finish executing. At that point, the barrier block executes by itself. Upon completion, the queue resumes its normal execution behavior.
dispatch_barrier_async
dispatch_barrier_async_f
dispatch_barrier_sync
dispatch_barrier_sync_f
Managing Dispatch Sources
GCD provides a suite of dispatch sources—interfaces for monitoring (low-level system objects such as Unix descriptors, Mach ports, Unix signals, VFS nodes, and so forth) for activity. and submitting event handlers to dispatch queues when such activity occurs. When an event occurs, the dispatch source submits your task code asynchronously to the specified dispatch queue for processing.
dispatch_source_cancel
dispatch_source_create
dispatch_source_get_data
dispatch_source_get_handle
dispatch_source_get_mask
dispatch_source_merge_data
dispatch_source_set_registration_handler
dispatch_source_set_registration_handler_f
dispatch_source_set_cancel_handler
dispatch_source_set_cancel_handler_f
dispatch_source_set_event_handler
dispatch_source_set_event_handler_f
dispatch_source_set_timer
dispatch_source_testcancel
Using the Dispatch I/O Convenience API
The dispatch I/O convenience API lets you perform asynchronous read and write operations on file descriptors. This API supports stream-based semantics for accessing the contents of the file-descriptor.
dispatch_read
dispatch_write
Using the Dispatch I/O Channel API
The dispatch I/O channel API lets you manage file descriptor–based operations. This API supports both stream-based and random-access semantics for accessing the contents of the file descriptor.
dispatch_io_create
dispatch_io_create_with_path
dispatch_io_create_with_io
dispatch_io_read
dispatch_io_write
dispatch_io_close
dispatch_io_barrier
dispatch_io_set_high_water
dispatch_io_set_low_water
dispatch_io_set_interval
dispatch_io_get_descriptor
Managing Dispatch Data Objects
Dispatch data objects present an interface for managing a memory-based data buffer. Clients accessing the data buffer see it as a contiguous block of memory, but internally the buffer may be comprised of multiple discontiguous blocks of memory.
dispatch_data_create
dispatch_data_get_size
dispatch_data_create_map
dispatch_data_create_concat
dispatch_data_create_subrange
dispatch_data_apply
dispatch_data_copy_region
Managing Time
dispatch_time
dispatch_walltime
Managing Queue-Specific Context Data
dispatch_queue_set_specific
dispatch_queue_get_specific
dispatch_get_specific
Data Types
dispatch_block_t
dispatch_function_t
dispatch_group_t
dispatch_object_t
dispatch_once_t
dispatch_queue_t
dispatch_time_t
dispatch_source_type_t
dispatch_fd_t
dispatch_data_t
dispatch_data_applier_t
dispatch_io_t
dispatch_io_handler_t
dispatch_io_type_t
dispatch_io_close_flags_t
dispatch_io_interval_flags_t
Constants
dispatch_queue_priority_t
dispatch_source_mach_send_flags_t
dispatch_source_proc_flags_t
dispatch_source_vnode_flags_t
dispatch_source_memorypressure_flags_t
Data Object Constants
Data Destructor Constants
Dispatch Queue Types
Dispatch Source Type Constants
Dispatch Time Constants
Time Multiplier Constants
Dispatch I/O Channel Types
Channel Closing Options
Channel Configuration Options