queue.h
IntroductionDispatch is an abstract model for expressing concurrency via simple but powerful API. At the core, dispatch provides serial FIFO queues to which blocks may be submitted. Blocks submitted to these dispatch queues are invoked on a pool of threads fully managed by the system. No guarantee is made regarding which thread a block will be invoked on; however, it is guaranteed that only one block submitted to the FIFO dispatch queue will be invoked at a time. When multiple queues have blocks to be processed, the system is free to allocate additional threads to invoke the blocks concurrently. When the queues become empty, these threads are automatically released. GroupsDispatch Barrier APIThe dispatch barrier API is a mechanism for submitting barrier blocks to a dispatch queue, analogous to the dispatch_async()/dispatch_sync() API. It enables the implementation of efficient reader/writer schemes. Barrier blocks only behave specially when submitted to queues created with the DISPATCH_QUEUE_CONCURRENT attribute; on such a queue, a barrier block will not run until all blocks submitted to the queue earlier have completed, and any blocks submitted to the queue after a barrier block will not run until the barrier block has completed. When submitted to a a global queue or to a queue not created with the DISPATCH_QUEUE_CONCURRENT attribute, barrier blocks behave identically to blocks submitted with the dispatch_async()/dispatch_sync() API. Group members:
Dispatch queue-specific contextsThis API allows different subsystems to associate context to a shared queue without risk of collision and to retrieve that context from blocks executing on that queue or any of its child queues in the target queue hierarchy. Group members:
Dispatch assertion API
This API asserts at runtime that code is executing in (or out of) the context of a given queue. It can be used to check that a block accessing a resource does so from the proper queue protecting the resource. It also can be used to verify that a block that could cause a deadlock if run on a given queue never executes on that queue. Group members:
Functions
dispatch_afterSchedule a block for execution on a given queue at a specified time. #if (defined( 1)) ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL2 DISPATCH_NONNULL3 void dispatch_after( dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block); #endif Parameters
DiscussionPassing DISPATCH_TIME_NOW as the "when" parameter is supported, but not as optimal as calling dispatch_async() instead. Passing DISPATCH_TIME_FOREVER is undefined. dispatch_after_fSchedule a function for execution on a given queue at a specified time. ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL2 DISPATCH_NONNULL4 void dispatch_after_f( dispatch_time_t when, dispatch_queue_t queue, void *_Nullable context, dispatch_function_t work); Parameters
DiscussionSee dispatch_after() for details. dispatch_applySubmits a block to a dispatch queue for parallel invocation. #if (defined( 1)) ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL3 void dispatch_apply( size_t iterations, dispatch_queue_t DISPATCH_APPLY_QUEUE_ARG_NULLABILITY queue, DISPATCH_NOESCAPE void (^block)( size_t)); #endif Parameters
DiscussionSubmits a block to a dispatch queue for parallel invocation. This function waits for the task block to complete before returning. If the specified queue is concurrent, the block may be invoked concurrently, and it must therefore be reentrant safe. Each invocation of the block will be passed the current index of iteration. dispatch_apply_fSubmits a function to a dispatch queue for parallel invocation. ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL4 void dispatch_apply_f( size_t iterations, dispatch_queue_t DISPATCH_APPLY_QUEUE_ARG_NULLABILITY queue, void *_Nullable context, void (*work)( void *_Nullable, size_t)); Parameters
DiscussionSee dispatch_apply() for details. dispatch_assert_queueVerifies that the current block is executing on a given dispatch queue. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) DISPATCH_NONNULL1 void dispatch_assert_queue( dispatch_queue_t queue) DISPATCH_ALIAS_V2( dispatch_assert_queue); ParametersDiscussionSome code expects to be run on a specific dispatch queue. This function verifies that that expectation is true. If the currently executing block was submitted to the specified queue or to any queue targeting it (see dispatch_set_target_queue()), this function returns. If the currently executing block was submitted with a synchronous API (dispatch_sync(), dispatch_barrier_sync(), ...), the context of the submitting block is also evaluated (recursively). If a synchronously submitting block is found that was itself submitted to the specified queue or to any queue targeting it, this function returns. Otherwise this function asserts: it logs an explanation to the system log and terminates the application. Passing the result of dispatch_get_main_queue() to this function verifies that the current block was submitted to the main queue, or to a queue targeting it, or is running on the main thread (in any context). When dispatch_assert_queue() is called outside of the context of a submitted block (for example from the context of a thread created manually with pthread_create()) then this function will also assert and terminate the application. The variant dispatch_assert_queue_debug() is compiled out when the preprocessor macro NDEBUG is defined. (See also assert(3)). dispatch_assert_queue_barrierVerifies that the current block is executing on a given dispatch queue, and that the block acts as a barrier on that queue. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) DISPATCH_NONNULL1 void dispatch_assert_queue_barrier( dispatch_queue_t queue); ParametersDiscussionThis behaves exactly like dispatch_assert_queue(), with the additional check that the current block acts as a barrier on the specified queue, which is always true if the specified queue is serial (see DISPATCH_BLOCK_BARRIER or dispatch_barrier_async() for details). The variant dispatch_assert_queue_barrier_debug() is compiled out when the preprocessor macro NDEBUG is defined. (See also assert()). dispatch_assert_queue_notVerifies that the current block is not executing on a given dispatch queue. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) DISPATCH_NONNULL1 void dispatch_assert_queue_not( dispatch_queue_t queue) DISPATCH_ALIAS_V2( dispatch_assert_queue_not); ParametersDiscussionThis function is the equivalent of dispatch_queue_assert() with the test for equality inverted. That means that it will terminate the application when dispatch_queue_assert() would return, and vice-versa. See discussion there. The variant dispatch_assert_queue_not_debug() is compiled out when the preprocessor macro NDEBUG is defined. (See also assert(3)). dispatch_asyncSubmits a block for asynchronous execution on a dispatch queue. #if (defined( 1)) ( macos( 10.6), ios( 4.0)) void dispatch_async( dispatch_queue_t queue, dispatch_block_t block); #endif Parameters
DiscussionThe dispatch_async() function is the fundamental mechanism for submitting blocks to a dispatch queue. Calls to dispatch_async() always return immediately after the block has been submitted, and never wait for the block to be invoked. The target queue determines whether the block will be invoked serially or concurrently with respect to other blocks submitted to that same queue. Serial queues are processed concurrently with respect to each other. dispatch_async_fSubmits a function for asynchronous execution on a dispatch queue. ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL1 DISPATCH_NONNULL3 void dispatch_async_f( dispatch_queue_t queue, void *_Nullable context, dispatch_function_t work); Parameters
DiscussionSee dispatch_async() for details. dispatch_barrier_asyncSubmits a barrier block for asynchronous execution on a dispatch queue. #if (defined( 1)) ( macos( 10.7), ios( 4.3)) void dispatch_barrier_async( dispatch_queue_t queue, dispatch_block_t block); #endif Parameters
DiscussionSubmits a block to a dispatch queue like dispatch_async(), but marks that block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues). See dispatch_async() for details. dispatch_barrier_async_fSubmits a barrier function for asynchronous execution on a dispatch queue. ( macos( 10.7), ios( 4.3)) DISPATCH_NONNULL1 DISPATCH_NONNULL3 void dispatch_barrier_async_f( dispatch_queue_t queue, void *_Nullable context, dispatch_function_t work); Parameters
DiscussionSubmits a function to a dispatch queue like dispatch_async_f(), but marks that function as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues). See dispatch_async_f() for details. dispatch_barrier_syncSubmits a barrier block for synchronous execution on a dispatch queue. #if (defined( 1)) ( macos( 10.7), ios( 4.3)) void dispatch_barrier_sync( dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block); #endif ParametersDiscussionSubmits a block to a dispatch queue like dispatch_sync(), but marks that block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues). See dispatch_sync() for details. dispatch_barrier_sync_fSubmits a barrier function for synchronous execution on a dispatch queue. ( macos( 10.7), ios( 4.3)) DISPATCH_NONNULL1 DISPATCH_NONNULL3 void dispatch_barrier_sync_f( dispatch_queue_t queue, void *_Nullable context, dispatch_function_t work); Parameters
DiscussionSubmits a function to a dispatch queue like dispatch_sync_f(), but marks that fuction as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues). See dispatch_sync_f() for details. dispatch_get_current_queueReturns the queue on which the currently executing block is running. ( "unsupported interface", macos( 10.6, 10.9), ios( 4.0, 6.0)) dispatch_queue_t dispatch_get_current_queue( void); Return ValueReturns the current queue. DiscussionReturns the queue on which the currently executing block is running. When dispatch_get_current_queue() is called outside of the context of a submitted block, it will return the default concurrent queue. Recommended for debugging and logging purposes only: The code must not make any assumptions about the queue returned, unless it is one of the global queues or a queue the code has itself created. The code must not assume that synchronous execution onto a queue is safe from deadlock if that queue is not the one returned by dispatch_get_current_queue(). When dispatch_get_current_queue() is called on the main thread, it may or may not return the same value as dispatch_get_main_queue(). Comparing the two is not a valid way to test whether code is executing on the main thread (see dispatch_assert_queue() and dispatch_assert_queue_not()). This function is deprecated and will be removed in a future release. dispatch_get_global_queueReturns a well-known global concurrent queue of a given quality of service class. ( macos( 10.6), ios( 4.0)) DISPATCH_CONST dispatch_queue_global_t dispatch_get_global_queue( intptr_t identifier, uintptr_t flags); Parameters
Return ValueReturns the requested global queue or NULL if the requested global queue does not exist. DiscussionSee dispatch_queue_global_t. dispatch_get_main_queueReturns the default queue that is bound to the main thread. DISPATCH_CONST dispatch_queue_main_t dispatch_get_main_queue( void) Return ValueReturns the main queue. This queue is created automatically on behalf of the main thread before main() is called. DiscussionIn order to invoke blocks submitted to the main queue, the application must call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main thread. The main queue is meant to be used in application context to interact with the main thread and the main runloop. Because the main queue doesn't behave entirely like a regular serial queue, it may have unwanted side-effects when used in processes that are not UI apps (daemons). For such processes, the main queue should be avoided. See dispatch_get_specificReturns the current subsystem-specific context for a key unique to the subsystem. ( macos( 10.7), ios( 5.0)) void *_Nullable dispatch_get_specific( const void *key); ParametersReturn ValueThe context for the specified key or NULL if no context was found. DiscussionWhen called from a block executing on a queue, returns the context for the specified key if it has been set on the queue, otherwise returns the result of dispatch_get_specific() executed on the queue's target queue or NULL if the current queue is a global concurrent queue. dispatch_mainExecute blocks submitted to the main queue. ( macos( 10.6), ios( 4.0)) DISPATCH_NORETURN void dispatch_main( void); DiscussionThis function "parks" the main thread and waits for blocks to be submitted to the main queue. This function never returns. Applications that call NSApplicationMain() or CFRunLoopRun() on the main thread do not need to call dispatch_main(). dispatch_queue_attr_make_initially_inactiveReturns an attribute value which may be provided to dispatch_queue_create() or dispatch_queue_create_with_target(), in order to make the created queue initially inactive. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) dispatch_queue_attr_t dispatch_queue_attr_make_initially_inactive( dispatch_queue_attr_t _Nullable attr); ParametersReturn ValueReturns an attribute value which may be provided to dispatch_queue_create() and dispatch_queue_create_with_target(). The new value combines the attributes specified by the 'attr' parameter with the initially inactive attribute. DiscussionDispatch queues may be created in an inactive state. Queues in this state have to be activated before any blocks associated with them will be invoked. A queue in inactive state cannot be deallocated, dispatch_activate() must be called before the last reference to a queue created with this attribute is released. The target queue of a queue in inactive state can be changed using dispatch_set_target_queue(). Change of target queue is no longer permitted once an initially inactive queue has been activated. dispatch_queue_attr_make_with_autorelease_frequencyReturns a dispatch queue attribute value with the autorelease frequency set to the specified value. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) dispatch_queue_attr_t dispatch_queue_attr_make_with_autorelease_frequency( dispatch_queue_attr_t _Nullable attr, dispatch_autorelease_frequency_t frequency); ParametersReturn ValueReturns an attribute value which may be provided to dispatch_queue_create() or NULL if an invalid autorelease frequency was requested. This new value combines the attributes specified by the 'attr' parameter and the chosen autorelease frequency. DiscussionWhen a queue uses the per-workitem autorelease frequency (either directly
or inherithed from its target queue), any block submitted asynchronously to
this queue (via dispatch_async(), dispatch_barrier_async(),
dispatch_group_notify(), etc...) is executed as if surrounded by a individual
Objective-C Autorelease frequency has no effect on blocks that are submitted synchronously to a queue (via dispatch_sync(), dispatch_barrier_sync()). The global concurrent queues have the DISPATCH_AUTORELEASE_FREQUENCY_NEVER behavior. Manually created dispatch queues use DISPATCH_AUTORELEASE_FREQUENCY_INHERIT by default. Queues created with this attribute cannot change target queues after having been activated. See dispatch_set_target_queue() and dispatch_activate(). dispatch_queue_attr_make_with_qos_classReturns an attribute value which may be provided to dispatch_queue_create() or dispatch_queue_create_with_target(), in order to assign a QOS class and relative priority to the queue. ( macos( 10.10), ios( 8.0)) dispatch_queue_attr_t dispatch_queue_attr_make_with_qos_class( dispatch_queue_attr_t _Nullable attr, dispatch_qos_class_t qos_class, int relative_priority); Parameters
Return ValueReturns an attribute value which may be provided to dispatch_queue_create() and dispatch_queue_create_with_target(), or NULL if an invalid QOS class was requested. The new value combines the attributes specified by the 'attr' parameter and the new QOS class and relative priority. DiscussionWhen specified in this manner, the QOS class and relative priority take precedence over those inherited from the dispatch queue's target queue (if any) as long that does not result in a lower QOS class and relative priority. The global queue priorities map to the following QOS classes: - DISPATCH_QUEUE_PRIORITY_HIGH: QOS_CLASS_USER_INITIATED - DISPATCH_QUEUE_PRIORITY_DEFAULT: QOS_CLASS_DEFAULT - DISPATCH_QUEUE_PRIORITY_LOW: QOS_CLASS_UTILITY - DISPATCH_QUEUE_PRIORITY_BACKGROUND: QOS_CLASS_BACKGROUND Example:
The QOS class and relative priority set this way on a queue have no effect on blocks that are submitted synchronously to a queue (via dispatch_sync(), dispatch_barrier_sync()). dispatch_queue_createCreates a new dispatch queue to which blocks may be submitted. ( macos( 10.6), ios( 4.0)) DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED dispatch_queue_t dispatch_queue_create( const char *_Nullable label, dispatch_queue_attr_t _Nullable attr); ParametersReturn ValueThe newly created dispatch queue. DiscussionDispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute invoke blocks serially in FIFO order. Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may invoke blocks concurrently (similarly to the global concurrent queues, but potentially with more overhead), and support barrier blocks submitted with the dispatch barrier API, which e.g. enables the implementation of efficient reader-writer schemes. When a dispatch queue is no longer needed, it should be released with dispatch_release(). Note that any pending blocks submitted asynchronously to a queue will hold a reference to that queue. Therefore a queue will not be deallocated until all pending blocks have finished. Passing the result of the dispatch_queue_attr_make_with_qos_class() function to the attr parameter of this function allows a quality of service class and relative priority to be specified for the newly created queue. The quality of service class so specified takes precedence over the quality of service class of the newly created dispatch queue's target queue (if any) as long that does not result in a lower QOS class and relative priority. When no quality of service class is specified, the target queue of a newly created dispatch queue is the default priority global concurrent queue. dispatch_queue_create_with_targetCreates a new dispatch queue with a specified target queue. ( macos( 10.12), ios( 10.0), tvos( 10.0), watchos( 3.0)) DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED dispatch_queue_t dispatch_queue_create_with_target( const char *_Nullable label, dispatch_queue_attr_t _Nullable attr, dispatch_queue_t _Nullable target) DISPATCH_ALIAS_V2( dispatch_queue_create_with_target); Parameters
Return ValueThe newly created dispatch queue. DiscussionDispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute invoke blocks serially in FIFO order. Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may invoke blocks concurrently (similarly to the global concurrent queues, but potentially with more overhead), and support barrier blocks submitted with the dispatch barrier API, which e.g. enables the implementation of efficient reader-writer schemes. When a dispatch queue is no longer needed, it should be released with dispatch_release(). Note that any pending blocks submitted asynchronously to a queue will hold a reference to that queue. Therefore a queue will not be deallocated until all pending blocks have finished. When using a dispatch queue attribute @a attr specifying a QoS class (derived from the result of dispatch_queue_attr_make_with_qos_class()), passing the result of dispatch_get_global_queue() in @a target will ignore the QoS class of that global queue and will use the global queue with the QoS class specified by attr instead. Queues created with dispatch_queue_create_with_target() cannot have their target queue changed, unless created inactive (See dispatch_queue_attr_make_initially_inactive()), in which case the target queue can be changed until the newly created queue is activated with dispatch_activate(). dispatch_queue_get_labelReturns the label of the given queue, as specified when the queue was created, or the empty string if a NULL label was specified. Passing DISPATCH_CURRENT_QUEUE_LABEL will return the label of the current queue. ( macos( 10.6), ios( 4.0)) const char * dispatch_queue_get_label( dispatch_queue_t _Nullable queue); ParametersReturn ValueThe label of the queue. dispatch_queue_get_qos_classReturns the QOS class and relative priority of the given queue. ( macos( 10.10), ios( 8.0)) DISPATCH_NONNULL1 dispatch_qos_class_t dispatch_queue_get_qos_class( dispatch_queue_t queue, int *_Nullable relative_priority_ptr); ParametersReturn ValueA QOS class value: - QOS_CLASS_USER_INTERACTIVE - QOS_CLASS_USER_INITIATED - QOS_CLASS_DEFAULT - QOS_CLASS_UTILITY - QOS_CLASS_BACKGROUND - QOS_CLASS_UNSPECIFIED DiscussionIf the given queue was created with an attribute value returned from dispatch_queue_attr_make_with_qos_class(), this function returns the QOS class and relative priority specified at that time; for any other attribute value it returns a QOS class of QOS_CLASS_UNSPECIFIED and a relative priority of 0. If the given queue is one of the global queues, this function returns its assigned QOS class value as documented under dispatch_get_global_queue() and a relative priority of 0; in the case of the main queue it returns the QOS value provided by qos_class_main() and a relative priority of 0. dispatch_queue_get_specificReturns the subsystem-specific context associated with a dispatch queue, for a key unique to the subsystem. ( macos( 10.7), ios( 5.0)) DISPATCH_NONNULL1 void *_Nullable dispatch_queue_get_specific( dispatch_queue_t queue, const void *key); Parameters
Return ValueThe context for the specified key or NULL if no context was found. DiscussionReturns the context for the specified key if it has been set on the specified queue. dispatch_queue_set_specificAssociates a subsystem-specific context with a dispatch queue, for a key unique to the subsystem. ( macos( 10.7), ios( 5.0)) DISPATCH_NONNULL1 void dispatch_queue_set_specific( dispatch_queue_t queue, const void *key, void *_Nullable context, dispatch_function_t _Nullable destructor); Parameters
DiscussionThe specified destructor will be invoked with the context on the default priority global concurrent queue when a new context is set for the same key, or after all references to the queue have been released. dispatch_set_target_queueSets the target queue for the given object. ( macos( 10.6), ios( 4.0)) void dispatch_set_target_queue( dispatch_object_t object, dispatch_queue_t _Nullable queue); Parameters
DiscussionAn object's target queue is responsible for processing the object. When no quality of service class and relative priority is specified for a dispatch queue at the time of creation, a dispatch queue's quality of service class is inherited from its target queue. The dispatch_get_global_queue() function may be used to obtain a target queue of a specific quality of service class, however the use of dispatch_queue_attr_make_with_qos_class() is recommended instead. Blocks submitted to a serial queue whose target queue is another serial queue will not be invoked concurrently with blocks submitted to the target queue or to any other queue with that same target queue. The result of introducing a cycle into the hierarchy of target queues is undefined. A dispatch source's target queue specifies where its event handler and cancellation handler blocks will be submitted. A dispatch I/O channel's target queue specifies where where its I/O operations are executed. If the channel's target queue's priority is set to DISPATCH_QUEUE_PRIORITY_BACKGROUND, then the I/O operations performed by dispatch_io_read() or dispatch_io_write() on that queue will be throttled when there is I/O contention. For all other dispatch object types, the only function of the target queue is to determine where an object's finalizer function is invoked. In general, changing the target queue of an object is an asynchronous operation that doesn't take effect immediately, and doesn't affect blocks already associated with the specified object. However, if an object is inactive at the time dispatch_set_target_queue() is called, then the target queue change takes effect immediately, and will affect blocks already associated with the specified object. After an initially inactive object has been activated, calling dispatch_set_target_queue() results in an assertion and the process being terminated. If a dispatch queue is active and targeted by other dispatch objects, changing its target queue results in undefined behavior. dispatch_syncSubmits a block for synchronous execution on a dispatch queue. #if (defined( 1)) ( macos( 10.6), ios( 4.0)) void dispatch_sync( dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block); #endif ParametersDiscussionSubmits a workitem to a dispatch queue like dispatch_async(), however dispatch_sync() will not return until the workitem has finished. Work items submitted to a queue with dispatch_sync() do not observe certain queue attributes of that queue when invoked (such as autorelease frequency and QOS class). Calls to dispatch_sync() targeting the current queue will result in dead-lock. Use of dispatch_sync() is also subject to the same multi-party dead-lock problems that may result from the use of a mutex. Use of dispatch_async() is preferred. Unlike dispatch_async(), no retain is performed on the target queue. Because calls to this function are synchronous, the dispatch_sync() "borrows" the reference of the caller. As an optimization, dispatch_sync() invokes the workitem on the thread which submitted the workitem, except when the passed queue is the main queue or a queue targetting it (See dispatch_queue_main_t, dispatch_set_target_queue()). dispatch_sync_fSubmits a function for synchronous execution on a dispatch queue. ( macos( 10.6), ios( 4.0)) DISPATCH_NONNULL1 DISPATCH_NONNULL3 void dispatch_sync_f( dispatch_queue_t queue, void *_Nullable context, dispatch_function_t work); Parameters
DiscussionSee dispatch_sync() for details. Typedefs
dispatch_autorelease_frequency_ttypedef enum dispatch_autorelease_frequency dispatch_autorelease_frequency_t; Constants
DiscussionValues to pass to the dispatch_queue_attr_make_with_autorelease_frequency() function. See
dispatch_qos_class_t#if ( __has_include(<sys/qos.h>)) typedef qos_class_t dispatch_qos_class_t; #else typedef unsigned int dispatch_qos_class_t; #endif DiscussionAlias for qos_class_t type. dispatch_queue_attr_tAttribute for dispatch queues. typedef struct dispatch_queue_attr_s *dispatch_queue_attr_t; dispatch_queue_concurrent_tDispatch concurrent queues invoke workitems submitted to them concurrently, and admit a notion of barrier workitems. #if ( defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)) typedef struct dispatch_lane_s *dispatch_queue_concurrent_t; #else typedef dispatch_queue_t dispatch_queue_concurrent_t; #endif DiscussionDispatch concurrent queues are lightweight objects to which regular and barrier workitems may be submited. Barrier workitems are invoked in exclusion of any other kind of workitem in FIFO order. Regular workitems can be invoked concurrently for the same concurrent queue, in any order. However, regular workitems will not be invoked before any barrier workitem submited ahead of them has been invoked. In other words, if a serial queue is equivalent to a mutex in the Dispatch world, a concurrent queue is equivalent to a reader-writer lock, where regular items are readers and barriers are writers. Concurrent queues are created by passing a dispatch queue attribute derived from DISPATCH_QUEUE_CONCURRENT to dispatch_queue_create_with_target(). Caveat: Dispatch concurrent queues at this time do not implement priority inversion avoidance when lower priority regular workitems (readers) are being invoked and are preventing a higher priority barrier (writer) from being invoked. dispatch_queue_global_tDispatch global concurrent queues are an abstraction around the system thread pool which invokes workitems that are submitted to dispatch queues. #if ( defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)) typedef struct dispatch_queue_global_s *dispatch_queue_global_t; #else typedef dispatch_queue_t dispatch_queue_global_t; #endif DiscussionDispatch global concurrent queues provide buckets of priorities on top of the thread pool the system manages. The system will decide how many threads to allocate to this pool depending on demand and system load. In particular, the system tries to maintain a good level of concurrency for this resource, and will create new threads when too many existing worker threads block in system calls. The global concurrent queues are a shared resource and as such it is the responsiblity of every user of this resource to not submit an unbounded amount of work to this pool, especially work that may block, as this can cause the system to spawn very large numbers of threads (aka. thread explosion). Work items submitted to the global concurrent queues have no ordering guarantee with respect to the order of submission, and workitems submitted to these queues may be invoked concurrently. Dispatch global concurrent queues are well-known global objects that are returned by dispatch_get_global_queue(). These objects cannot be modified. Calls to dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will have no effect when used with queues of this type. dispatch_queue_main_tThe type of the default queue that is bound to the main thread. #if ( defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)) typedef struct dispatch_queue_static_s *dispatch_queue_main_t; #else typedef dispatch_queue_serial_t dispatch_queue_main_t; #endif DiscussionThe main queue is a serial queue (See dispatch_queue_serial_t) which is bound to the main thread of an application. In order to invoke workitems submitted to the main queue, the application must call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main thread. The main queue is a well known global object that is made automatically on behalf of the main thread during process initialization and is returned by dispatch_get_main_queue(). This object cannot be modified. Calls to dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will have no effect when used on the main queue. dispatch_queue_priority_ttypedef long dispatch_queue_priority_t; Constants
DiscussionType of dispatch_queue_priority See Also dispatch_queue_serial_tDispatch serial queues invoke workitems submitted to them serially in FIFO order. #if ( defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)) typedef struct dispatch_lane_s *dispatch_queue_serial_t; #else typedef dispatch_queue_t dispatch_queue_serial_t; #endif DiscussionDispatch serial queues are lightweight objects to which workitems may be submitted to be invoked in FIFO order. A serial queue will only invoke one workitem at a time, but independent serial queues may each invoke their work items concurrently with respect to each other. Serial queues can target each other (See dispatch_set_target_queue()). The serial queue at the bottom of a queue hierarchy provides an exclusion context: at most one workitem submitted to any of the queues in such a hiearchy will run at any given time. Such hierarchies provide a natural construct to organize an application subsystem around. Serial queues are created by passing a dispatch queue attribute derived from DISPATCH_QUEUE_SERIAL to dispatch_queue_create_with_target(). dispatch_queue_tDispatch queues invoke workitems submitted to them. typedef struct dispatch_queue_s *dispatch_queue_t; DiscussionDispatch queues come in many flavors, the most common one being the dispatch serial queue (See dispatch_queue_serial_t). The system manages a pool of threads which process dispatch queues and invoke workitems submitted to them. Conceptually a dispatch queue may have its own thread of execution, and interaction between queues is highly asynchronous. Dispatch queues are reference counted via calls to dispatch_retain() and dispatch_release(). Pending workitems submitted to a queue also hold a reference to the queue until they have finished. Once all references to a queue have been released, the queue will be deallocated by the system. Macro Definitions
DISPATCH_APPLY_AUTOConstant to pass to dispatch_apply() or dispatch_apply_f() to request that the system automatically use worker threads that match the configuration of the current thread as closely as possible. #define DISPATCH_APPLY_AUTO DiscussionWhen submitting a block for parallel invocation, passing this constant as the queue argument will automatically use the global concurrent queue that matches the Quality of Service of the caller most closely. No assumptions should be made about which global concurrent queue will actually be used. Using this constant deploys backward to macOS 10.9, iOS 7.0 and any tvOS or watchOS version. DISPATCH_CURRENT_QUEUE_LABEL#define DISPATCH_CURRENT_QUEUE_LABEL NULL DiscussionConstant to pass to the dispatch_queue_get_label() function to retrieve the label of the current queue. DISPATCH_QUEUE_CONCURRENT#define DISPATCH_QUEUE_CONCURRENT \ DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \ _dispatch_queue_attr_concurrent) DiscussionAn attribute that can be used to create a dispatch queue that may invoke blocks concurrently and supports barrier blocks submitted with the dispatch barrier API. See dispatch_queue_concurrent_t. DISPATCH_QUEUE_CONCURRENT_INACTIVE#define DISPATCH_QUEUE_CONCURRENT_INACTIVE \ dispatch_queue_attr_make_initially_inactive( DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \ _dispatch_queue_attr_concurrent)) DiscussionAn attribute that can be used to create a dispatch queue that may invoke blocks concurrently and supports barrier blocks submitted with the dispatch barrier API, and that is initially inactive. See dispatch_queue_attr_make_initially_inactive(). DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL#define DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL \ dispatch_queue_attr_make_with_autorelease_frequency(\ DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \ _dispatch_queue_attr_concurrent), DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM) DiscussionA dispatch queue created with this attribute may invokes blocks concurrently
and supports barrier blocks submitted with the dispatch barrier API. It also
surrounds execution of any block submitted asynchronously to it with the
equivalent of a individual Objective-C See dispatch_queue_attr_make_with_autorelease_frequency(). DISPATCH_QUEUE_PRIORITY_BACKGROUND#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN DiscussionType of dispatch_queue_priority See Also DISPATCH_QUEUE_PRIORITY_DEFAULT#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 DiscussionType of dispatch_queue_priority See Also DISPATCH_QUEUE_PRIORITY_HIGH#define DISPATCH_QUEUE_PRIORITY_HIGH 2 DiscussionType of dispatch_queue_priority See Also DISPATCH_QUEUE_PRIORITY_LOW#define DISPATCH_QUEUE_PRIORITY_LOW DiscussionType of dispatch_queue_priority See Also DISPATCH_QUEUE_SERIAL#define DISPATCH_QUEUE_SERIAL NULL DiscussionAn attribute that can be used to create a dispatch queue that invokes blocks serially in FIFO order. See dispatch_queue_serial_t. DISPATCH_QUEUE_SERIAL_INACTIVEDiscussionAn attribute that can be used to create a dispatch queue that invokes blocks serially in FIFO order, and that is initially inactive. See dispatch_queue_attr_make_initially_inactive(). DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL#define DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL \ dispatch_queue_attr_make_with_autorelease_frequency(\ NULL, DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM) DiscussionA dispatch queue created with this attribute invokes blocks serially in FIFO
order, and surrounds execution of any block submitted asynchronously to it
with the equivalent of a individual Objective-C See dispatch_queue_attr_make_with_autorelease_frequency(). DISPATCH_TARGET_QUEUE_DEFAULT#define DISPATCH_TARGET_QUEUE_DEFAULT NULL DiscussionConstant to pass to the dispatch_queue_create_with_target(), dispatch_set_target_queue() and dispatch_source_create() functions to indicate that the default target queue for the object type in question should be used. |