Grand Central Dispatch Explained

Author:Dave Zarzycki
Developer:Apple Inc.
Programming Language:C
Operating System:Mac OS X 10.6 (2009) and later, iOS 4.0 and later,[1] watchOS, tvOS, FreeBSD
Genre:System Utility
License:Apache 2.0
Website:https://apple.github.io/swift-corelibs-libdispatch/

Grand Central Dispatch (GCD or libdispatch) is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems.[2] It is an implementation of task parallelism based on the thread pool pattern. The fundamental idea is to move the management of the thread pool out of the hands of the developer, and closer to the operating system. The developer injects "work packages" into the pool oblivious of the pool's architecture. This model improves simplicity, portability and performance.

GCD was first released with Mac OS X 10.6, and is also available with iOS 4 and above. The name "Grand Central Dispatch" is a reference to Grand Central Terminal.

The source code for the library that provides the implementation of GCD's services, libdispatch, was released by Apple under the Apache License on September 10, 2009.[3] It has been ported[4] to FreeBSD 8.1+,[5] MidnightBSD 0.3+,[6] Linux, and Solaris.[7] [8] Attempts in 2011 to make libdispatch work on Windows were not merged into upstream.[9] [10] Apple has its own port of libdispatch.dll for Windows shipped with Safari and iTunes, but no SDK is provided.

Since around 2017, the original libdispatch repository hosted by Nick Hutchinson[11] was deprecated in favor of a version that is part of the Swift core library created in June 2016. The new version supports more platforms, notably including Windows.

Design

GCD works by allowing specific tasks in a program that can be run in parallel to be queued up for execution and, depending on availability of processing resources, scheduling them to execute on any of the available processor cores[12] [13] (referred to as "routing" by Apple).[14]

A task can be expressed either as a function or as a "block."[15] Blocks are an extension to the syntax of C, C++, and Objective-C programming languages that encapsulate code and data into a single object in a way similar to a closure.[12] GCD can still be used in environments where blocks are not available.[16]

Grand Central Dispatch still uses threads at the low level but abstracts them away from the programmer, who will not need to be concerned with as many details. Tasks in GCD are lightweight to create and queue; Apple states that 15 instructions are required to queue up a work unit in GCD, while creating a traditional thread could easily require several hundred instructions.[12]

A task in Grand Central Dispatch can be used either to create a work item that is placed in a queue or assign it to an event source. If a task is assigned to an event source, then a work unit is made from the block or function when the event triggers, and the work unit is placed in an appropriate queue. This is described by Apple as more efficient than creating a thread whose sole purpose is to wait on a single event triggering.

Features

The dispatch framework declares several data types and functions to create and manipulate them:

Libdispatch comes with its own object model, OS Object, that is partially compatible with the Objective-C model. As a result, its objects can be bridged toll-free to ObjC objects.[17]

Examples

Two examples that demonstrate the use of Grand Central Dispatch can be found in John Siracusa's Ars Technica Snow Leopard review.[18] Initially, a document-based application has a method called analyzeDocument which may do something like count the number of words and paragraphs in the document. Normally, this would be a quick process, and may be executed in the main thread without the user noticing a delay between pressing a button and the results showing.

- (IBAction)analyzeDocument:(NSButton *)sender If the document is large and analysis takes a long time to execute then the main thread will wait for the function to finish. If it takes long enough, the user will notice, and the application may even "beachball". The solution can be seen here:

- (IBAction)analyzeDocument:(NSButton *)sender Here, the call to [myDoc analyze] is placed inside a Block, which is then placed on one of the global concurrent queues. After it has finished running [myDoc analyze], a new block is placed on the main queue (on which the main thread of the application runs), which updates the GUI (This is necessary because the GUI can only be updated by the main thread). By making these two small changes, the developer has avoided a potential stall of the application as seen by the user, and allowed their application to make better use of hardware resources.

The second example is that of parallelising a for loop:

for (i = 0; i < count; i++) total = summarize(results, count);This code runs the do_work function count times, assigning the ith result to the ith element in the array results, and then calls summarize on array once the loop has ended. Unfortunately the work is computed sequentially, where it may not need to be. Assuming that do_work doesn't rely on the results of any of the other calls made to it, there is no reason why these calls cannot be made concurrently. This is how this would be done in GCD:

dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i));total = summarize(results, count);Here, dispatch_apply runs the block passed to it, count times, placing each invocation on a global queue, and passing each block invocation a different number from 0 to count-1. This will allow the OS to spread out the work as it sees fit, choosing the optimal number of threads to run on for the current hardware and system load. dispatch_apply does not return until all the blocks it places on the given queue have completed execution, so that it can be guaranteed that all the work inside the original loop has completed before calling summarize.

Programmers can create their own serial queues for tasks which they know must run serially but which may be executed on a separate thread. A new queue would be created like so:

dispatch_queue_t exampleQueue;exampleQueue = dispatch_queue_create("com.example.unique.identifier", NULL);

// exampleQueue may be used here.

dispatch_release(exampleQueue);

Care must be taken to avoid a dispatched block on a queue synchronously placing another block on the same queue as this is guaranteed to deadlock. Such code might do the following:dispatch_queue_t exampleQueue = dispatch_queue_create("com.example.unique.identifier", NULL);

dispatch_sync(exampleQueue, ^);

dispatch_release(exampleQueue);

Applications

GCD is used throughout macOS (beginning with 10.6 Snow Leopard), and Apple has encouraged its adoption by macOS application developers. FreeBSD developer Robert Watson announced the first adaptation of a major open source application, the Apache HTTP Server, to use GCD via the Apache GCD MPM (Multi-Processing Module) on May 11, 2010, in order to illustrate the programming model and how to integrate GCD into existing, large-scale multi-threaded, applications. His announcement observed that the GCD MPM had one third to half the number of lines as other threaded MPMs.[19] [20]

Internals

GCD is implemented by libdispatch, with support from pthreads non-POSIX extensions developed by Apple. Apple has changed the interface since its inception (in OS X 10.5) through the official launch of GCD (10.6), Mountain Lion (10.8) and Mavericks (10.9). The latest changes involve making the code supporting pthreads, both in user mode and kernel, private (with kernel pthread support reduced to shims only, and the actual workqueue implementation moved to a separate kernel extension).[21]

On other systems, libdispatch implements its own workqueue using the system's own event facilities (epoll, kevent, or Windows NT). On macOS, kevent is used with the kernel workqueue.

See also

External links

Notes and References

  1. Web site: Grand Central Dispatch (GCD) Reference. Apple Inc..
  2. https://www.apple.com/pr/library/2008/06/09snowleopard.html Apple Previews Mac OS X Snow Leopard to Developers
  3. Web site: Grand Central Dispatch. apple.github.io.
  4. Web site: GCD libdispatch w/Blocks support working on FreeBSD . September 28, 2009 . July 27, 2011 . https://web.archive.org/web/20110727053237/http://lists.macosforge.org/pipermail/libdispatch-dev/2009-September/000059.html . dead .
  5. Web site: FreeBSD Quarterly Status Report. www.freebsd.org.
  6. Web site: libdispatch. Just Journal.
  7. http://lists.macosforge.org/pipermail/libdispatch-dev/2011-April/000485.html libdispatch mailing list: "Porting status Linux"
  8. http://lists.macosforge.org/pipermail/libdispatch-dev/2011-April/000486.html libdispatch mailing list: "Porting status Solaris x86/64"
  9. http://lists.macosforge.org/pipermail/libdispatch-dev/2011-April/000510.html libdispatch mailing list: "libdispatch for Win32"
  10. http://lists.macosforge.org/pipermail/libdispatch-dev/2011-May/000515.html libdispatch mailing list: "Updates regarding the status of libdispatch on Windows"
  11. Web site: Hutchinson . Nicholas . libdispatch . GitHub . 15 January 2020 . 10 January 2020.
  12. Web site: Apple Technical Brief on Grand Central Dispatch . 2009-09-12 . bot: unknown . https://web.archive.org/web/20090920043909/http://images.apple.com/macosx/technology/docs/GrandCentral_TB_brief_20090903.pdf . September 20, 2009 . mdy-all .
  13. Book: Gagne, Abraham Silberschatz, Peter Baer Galvin, Greg. Operating system concepts. 2013. Wiley. Hoboken, N.J.. 9781118063330. 182–183. 9th.
  14. Web site: WWDC 2008: New in Mac OS X Snow Leopard. June 18, 2008. https://web.archive.org/web/20081017052319/http://www.roughlydrafted.com/2008/06/12/wwdc-2008-new-in-mac-os-x-snow-leopard/. October 17, 2008. dead.
  15. Web site: Grand Central Dispatch (GCD) Reference. September 13, 2009.
  16. Web site: GrandCentralDispatch - FreeBSD Wiki.
  17. Web site: Bugaev . Sergey . Mach-O linking and loading tricks . Darling development blog . July 24, 2018 . 15 January 2020.
  18. https://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/13 Mac OS X 10.6 Snow Leopard: the Ars Technica review
  19. http://lists.macosforge.org/pipermail/libdispatch-dev/2010-May/000352.html libdispatch-dev GCD MPM for Apache
  20. http://libdispatch.macosforge.org/trac/wiki/apache apache-libdispatch
  21. Web site: GCD Internals: The undocumented side of the Grand Central Dispatcher . Levin . Jonathan . 15 February 2014 . 17 March 2014.