Framework

Dispatch

Execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system.

Overview

Dispatch, also known as Grand Central Dispatch (GCD), contains language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in macOS, iOS, watchOS, and tvOS.

The BSD subsystem, Core Foundation, 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 to do 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.

Dispatch Objects and ARC

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 and release functions.

If you need to use retain and 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.

Topics

Queues and Tasks

dispatch_get_main_queue

Returns the serial dispatch queue associated with the application’s main thread.

dispatch_get_global_queue

Returns a system-defined global concurrent queue with the specified quality-of-service class.

Dispatch Queue

An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread..

Dispatch Work Item

The work you want to perform, encapsulated in a way that lets you attach a completion handle or execution dependencies.

Dispatch Group

A group of tasks that you monitor as a single unit.

Quality of Service

dispatch_qos_class_t

Quality-of-service classes that specify the priorities for executing tasks.

dispatch_queue_priority_t

The execution priority for tasks in a global concurrent queue.

System Event Monitoring

Dispatch Source

An object that coordinates the processing of specific low-level system events, such as file-system events, timers, and UNIX signals.

Dispatch I/O

An object that manages operations on a file descriptor using either stream-based or random-access semantics.

Dispatch Data

An object that manages a memory-based data buffer and exposes it as a contiguous block of memory.

Task Synchronization

Dispatch Semaphore

An object that controls access to a resource across multiple execution contexts through use of a traditional counting semaphore.

Dispatch Barrier

A synchronization point for tasks executing in a concurrent dispatch queue.

Time Constructs

dispatch_time

Creates a dispatch_time_t relative to the default clock or modifies an existing dispatch_time_t.

dispatch_walltime

Creates a dispatch_time_t using an absolute time according to the wall clock.

dispatch_time_t

An abstract representation of time.

DISPATCH_WALLTIME_NOW

The current time.

Dispatch Objects

Dispatch Objects

The basic behaviors supported by all dispatch types.

Protocols

OS_dispatch_source

Defines a common set of properties and methods that are shared with all dispatch source types.