<!--
{
  "documentType" : "article",
  "framework" : "Dispatch",
  "identifier" : "/documentation/Dispatch/dispatch-queue",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Dispatch Queue"
}
-->

# Dispatch Queue

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

## Discussion

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system. Except for the dispatch queue representing your app’s main thread, the system makes no guarantees about which thread it uses to execute a task.

You schedule work items synchronously or asynchronously. When you schedule a work item synchronously, your code waits until that item finishes execution. When you schedule a work item asynchronously, your code continues executing while the work item runs elsewhere.

> Important:
> Attempting to synchronously execute a work item on the main queue results in deadlock.

Dispatch queues provide minimal support for autoreleased objects by default. System APIs may return autoreleased objects to your code. For example, <doc://com.apple.documentation/documentation/Foundation/NSError> objects are often autoreleased. If you see memory pressure increase because of autoreleased objects created in your blocks, consider adding autorelease pools to those blocks to relieve the pressure. You can also configure the default autorelease behavior of any custom dispatch queues using the [`dispatch_queue_attr_make_with_autorelease_frequency`](/documentation/Dispatch/dispatch_queue_attr_make_with_autorelease_frequency) function at creation time.

### Avoiding Excessive Thread Creation

When designing tasks for concurrent execution, do not call methods that block the current thread of execution. When a task scheduled by a concurrent dispatch queue blocks a thread, the system creates additional threads to run other queued concurrent tasks. If too many tasks block, the system may run out of threads for your app.

Another way that apps consume too many threads is by creating too many private concurrent dispatch queues. Because each dispatch queue consumes thread resources, creating additional concurrent dispatch queues exacerbates the thread consumption problem. Instead of creating private concurrent queues, submit tasks to one of the global concurrent dispatch queues. For serial tasks, set the target of your serial queue to one of the global concurrent queues. That way, you can maintain the serialized behavior of the queue while minimizing the number of separate queues creating threads.

## Topics

### Creating a Dispatch Queue

[`dispatch_get_main_queue`](/documentation/Dispatch/dispatch_get_main_queue)

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

[`dispatch_get_global_queue`](/documentation/Dispatch/dispatch_get_global_queue)

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

[`dispatch_queue_create`](/documentation/Dispatch/dispatch_queue_create)

Creates a new dispatch queue to which you can submit blocks.

[`dispatch_queue_create_with_target`](/documentation/Dispatch/dispatch_queue_create_with_target)

Creates a new dispatch queue to which you can submit blocks.

[`DISPATCH_QUEUE_SERIAL`](/documentation/Dispatch/DISPATCH_QUEUE_SERIAL)

A dispatch queue that executes blocks serially in FIFO order.

[`DISPATCH_QUEUE_CONCURRENT`](/documentation/Dispatch/DISPATCH_QUEUE_CONCURRENT)

A dispatch queue that executes blocks concurrently.

[`dispatch_queue_t`](/documentation/Dispatch/dispatch_queue_t)

A lightweight object to which your application submits blocks for subsequent execution.

[`dispatch_queue_main_t`](/documentation/Dispatch/dispatch_queue_main_t)

A dispatch queue that is bound to the app’s main thread and executes tasks serially on that thread.

[`dispatch_queue_global_t`](/documentation/Dispatch/dispatch_queue_global_t)

A dispatch queue that executes tasks concurrently using threads from the global thread pool.

[`dispatch_queue_serial_t`](/documentation/Dispatch/dispatch_queue_serial_t)

A dispatch queue that executes tasks serially in first-in, first-out (FIFO) order.

[`dispatch_queue_concurrent_t`](/documentation/Dispatch/dispatch_queue_concurrent_t)

A dispatch queue that executes tasks concurrently and in any order, respecting any barriers that may be in place.

### Configuring Queue Execution Parameters

[`dispatch_queue_attr_t`](/documentation/Dispatch/dispatch_queue_attr_t)

Attributes describing the behaviors of a dispatch queue.

[`dispatch_queue_attr_make_with_qos_class`](/documentation/Dispatch/dispatch_queue_attr_make_with_qos_class)

Returns attributes suitable for creating a dispatch queue with the desired quality-of-service information.

[`dispatch_queue_get_qos_class`](/documentation/Dispatch/dispatch_queue_get_qos_class)

Returns the quality-of-service class for the specified queue.

[`dispatch_qos_class_t`](/documentation/Dispatch/dispatch_qos_class_t)

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

[`dispatch_queue_attr_make_initially_inactive`](/documentation/Dispatch/dispatch_queue_attr_make_initially_inactive)

Returns an attribute that configures a dispatch queue as initially inactive.

[`dispatch_queue_attr_make_with_autorelease_frequency`](/documentation/Dispatch/dispatch_queue_attr_make_with_autorelease_frequency)

Returns an attribute that specifies how the dispatch queue manages autorelease pools for the blocks it executes.

[`dispatch_autorelease_frequency_t`](/documentation/Dispatch/dispatch_autorelease_frequency_t)

### Executing Tasks Asynchronously

[`dispatch_async`](/documentation/Dispatch/dispatch_async)

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

[`dispatch_async_f`](/documentation/Dispatch/dispatch_async_f)

Submits an app-defined function for asynchronous execution on a dispatch queue and returns immediately.

[`dispatch_after`](/documentation/Dispatch/dispatch_after)

Enqueues a block for execution at the specified time.

[`dispatch_after_f`](/documentation/Dispatch/dispatch_after_f)

Enqueues an app-defined function for execution at the specified time.

[`dispatch_function_t`](/documentation/Dispatch/dispatch_function_t)

The prototype of functions submitted to dispatch queues.

[`dispatch_block_t`](/documentation/Dispatch/dispatch_block_t)

The prototype of blocks submitted to dispatch queues, which take no arguments and have no return value.

### Executing Tasks Synchronously

[`sync(execute:)`](/documentation/Dispatch/DispatchQueue/sync(execute:)-3segw)

Submits a block object for execution and returns after that block finishes executing.

[`dispatch_sync_f`](/documentation/Dispatch/dispatch_sync_f)

Submits an app-defined function for synchronous execution on a dispatch queue.

[`asyncAndWait(execute:)`](/documentation/Dispatch/DispatchQueue/asyncAndWait(execute:)-1udeu)

Submits a work item for execution and returns only after it finishes executing.

[`dispatch_async_and_wait_f`](/documentation/Dispatch/dispatch_async_and_wait_f)

Submits a function-based work item for execution and returns only after it finishes executing.

[`dispatch_barrier_async_and_wait`](/documentation/Dispatch/dispatch_barrier_async_and_wait)

Submits a work item for synchronous execution and marks the work as a barrier for subsequent concurrent tasks.

[`dispatch_barrier_async_and_wait_f`](/documentation/Dispatch/dispatch_barrier_async_and_wait_f)

Submits a function-based work item for synchronous execution and marks the work as a barrier for subsequent concurrent tasks.

### Executing a Task Only Once

[`dispatch_once`](/documentation/Dispatch/dispatch_once-c.func)

Executes a block object only once for the lifetime of an application.

[`dispatch_once_f`](/documentation/Dispatch/dispatch_once_f-c.func)

Executes an application-defined function only once for the lifetime of an application.

[`dispatch_once_t`](/documentation/Dispatch/dispatch_once_t)

A predicate for use with the `dispatch_once` function.

### Executing a Task in Parallel

[`dispatch_apply`](/documentation/Dispatch/dispatch_apply)

Submits a single block to the dispatch queue and causes the block to be executed the specified number of times.

[`dispatch_apply_f`](/documentation/Dispatch/dispatch_apply_f)

Submits a single function to the dispatch queue and causes the function to be executed the specified number of times.

[`DISPATCH_APPLY_AUTO`](/documentation/Dispatch/DISPATCH_APPLY_AUTO)

### Executing a Barrier Task

[`dispatch_barrier_async`](/documentation/Dispatch/dispatch_barrier_async)

Submits a barrier block for asynchronous execution and returns immediately.

[`dispatch_barrier_async_f`](/documentation/Dispatch/dispatch_barrier_async_f)

Submits a barrier function for asynchronous execution and returns immediately.

[`dispatch_barrier_sync`](/documentation/Dispatch/dispatch_barrier_sync)

Submits a barrier block object for execution and waits until that block completes.

[`dispatch_barrier_sync_f`](/documentation/Dispatch/dispatch_barrier_sync_f)

Submits a barrier function for execution and waits until that function completes.

[`dispatch_barrier_async_and_wait`](/documentation/Dispatch/dispatch_barrier_async_and_wait)

Submits a work item for synchronous execution and marks the work as a barrier for subsequent concurrent tasks.

[`dispatch_barrier_async_and_wait_f`](/documentation/Dispatch/dispatch_barrier_async_and_wait_f)

Submits a function-based work item for synchronous execution and marks the work as a barrier for subsequent concurrent tasks.

### Managing Queue Attributes

[`dispatch_queue_get_label`](/documentation/Dispatch/dispatch_queue_get_label)

Returns the label you assigned to the dispatch queue at creation time.

[`DISPATCH_CURRENT_QUEUE_LABEL`](/documentation/Dispatch/DISPATCH_CURRENT_QUEUE_LABEL)

Pass this constant to the [`dispatch_queue_get_label`](/documentation/Dispatch/dispatch_queue_get_label) function to retrieve the label of the current queue.

[`setTarget(queue:)`](/documentation/Dispatch/DispatchObject/setTarget(queue:))

Specifies the dispatch queue on which to perform work associated with the current object.

### Getting and Setting Contextual Data

[`dispatch_get_specific`](/documentation/Dispatch/dispatch_get_specific)

Returns the value for the key associated with the current dispatch queue.

[`dispatch_queue_set_specific`](/documentation/Dispatch/dispatch_queue_set_specific)

Sets the key/value data for the specified dispatch queue.

[`dispatch_queue_get_specific`](/documentation/Dispatch/dispatch_queue_get_specific)

Gets the value for the key associated with the specified dispatch queue.

### Managing the Main Dispatch Queue

[`dispatchMain()`](/documentation/Dispatch/dispatchMain())

Executes blocks submitted to the main queue.

### Testing the Execution Context

[`dispatch_assert_queue`](/documentation/Dispatch/dispatch_assert_queue)

Generates an assertion if the current block is not running on the specified dispatch queue.

[`dispatch_assert_queue_barrier`](/documentation/Dispatch/dispatch_assert_queue_barrier)

Generates an assertion if the current block is not running as a barrier on the specified dispatch queue.

[`dispatch_assert_queue_not`](/documentation/Dispatch/dispatch_assert_queue_not)

Generates an assertion if the current block is executing on the specified dispatch queue.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)
