Apple Releases Grand Central Dispatch as Open Source

One of the main new features in Apple’s new Snow Leopard operating system has been released as open source. Apple has released the code of the userland portion of its Grand Central Dispatch technology under the Apache License, version 2. Mac OS X also has kernel support for Grand Central Dispatch, which is also released as open source via the XNU project. While we’re at it, let’s take this opportunity to look into exactly what Grand Central Dispatch is.

What is Grand Central Dispatch?

Grand Central Dispatch is Apple’s answer to the problem of parallel programming. With modern computers containing ever more processing cores, you’d think that operating systems and applications would run ever faster. However, this is not the case because in order to benefit from these multiple processing cores, code has to be executed in different threads.

Sounds simple enough. Have your program use multiple threads so that each core can handle one of them, and bingo, each additional core equals benefit. Of course, it’s not that simple. Parallel programming is extremely difficult, and even the most talented of programmers will run into race conditions, deadlocks, and other difficulties, because different threads require access to the same data and memory space.

The BeOS was probably one of the first (the first?) desktop operating system which was designed from the ground up to run on multiple processors. By making extensive use of multithreading, the BeOS could squeeze every last ounce of power from the two processors inside the BeBox, resulting in a very impressive operating system which could do all sorts of fancy tricks in a time when the competition was just discovering the merits of protected memory. However, all this multithreading was quite harsh on programmers.

Without proper multithreading, applications today can seriously underutilise the power available to them. Singlethreaded applications will only utilise a single core, leading to all sorts of performance issues. This screenshot from John Siracusa’s excellent Snow Leopard review illustrates this point:

Tumbleweeds for 1-7.
And your wallet dies a little inside.

Grand Central Dispatch is Apple’s answer to make the life of programmers easier. Note that from this point onwards, I’m pretty much relying on whatever Siracusa wrote down. It’s not a new Cocoa framework, but a plain C library which is available for any of the C-based languages in Mac OS X: Objective-C, C++, and Objective-C++. You only need to add a #include <dispatch/dispatch.h>.

So, what, exactly, does GCD do? Siracusa put it like this:

The bottom line is that the optimal number of threads to put in flight at any given time is best determined by a single, globally aware entity. In Snow Leopard, that entity is GCD. It will keep zero threads in its pool if there are no queues that have tasks to run. As tasks are dequeued, GCD will create and dole out threads in a way that optimizes the use of the available hardware. GCD knows how many cores the system has, and it knows how many threads are currently executing tasks. When a queue no longer needs a thread, it’s returned to the pool where GCD can hand it out to another queue that has a task ready to be dequeued.

Basically, thread management is no longer something programmers have to do manually; programmers simply cannot know what the optimal thread count of their software is, as your system may be doing any number of things at any given time.

GCD is not about pervasive multithreading like the BeOS was. BeOS was about having each and every component run on its own concurrent thread, leading to difficulties in data sharing and locking. GCD, on the other hand, promotes a more hierarchical design, where you have one main application thread for user events and the interface, and any number of “worker threads” doing specific jobs as needed.

Siracusa presents the example of a word processor which has a button which analyses a document, and presents a number of statistics. On an average document, this takes less than a second, so it can easily be run on the main program thread without the user ever noticing it. However, what if you were to load up a very long and complicated document?

Suddenly, the analysis would take 15-30 seconds, and because it runs on the program’s main thread, the entire application will become unresponsive, the beach ball or hourglass will appear, and the user is bummed. This is where Grand Central Dispatch comes into play: using just two additional lines of code, a programmer can relegate the analysis to its own thread, without it interfering the execution of the program’s main thread. Application remains responsive, no beach balls and hourglasses, user is not bummed out. “No application-global objects, no thread management, no callbacks, no argument marshalling, no context objects, not even any additional variables,” Siracusa explains, “Behold, Grand Central Dispatch.”

It is important to note, however, that GCD doesn’t actually address the problem of parallelism in computer programming at all: programmers will still have to figure out for themselves which tasks can be run in parallel and when. What GCD does, however, is make the actual process of spinning off a task to its own thread easy.

Open source

The news now is that Apple has released the userspace implementation of Grand Central Dispatch as open source under version 2 of the Apache License (the kernel component is part of the open source XNU project). For portability purposes, kernel support is not necessary – compiler support for blocks, however, is. The blocks runtime is available as a component of the LLVM project.

Further reading:

67 Comments

  1. 2009-09-11 2:31 pm
    • 2009-09-11 2:42 pm
      • 2009-09-11 2:47 pm
        • 2009-09-11 3:01 pm
        • 2009-09-12 12:59 am
          • 2009-09-12 10:26 am
          • 2009-09-12 10:40 am
        • 2009-09-12 3:03 am
          • 2009-09-12 4:35 am
    • 2009-09-11 2:47 pm
    • 2009-09-11 3:20 pm
      • 2009-09-11 3:24 pm
      • 2009-09-11 5:53 pm
      • 2009-09-11 5:57 pm
        • 2009-09-11 6:04 pm
        • 2009-09-11 6:30 pm
        • 2009-09-11 6:35 pm
          • 2009-09-11 11:09 pm
          • 2009-09-12 1:49 pm
          • 2009-09-12 7:03 pm
          • 2009-09-13 1:46 pm
          • 2009-09-13 3:17 pm
      • 2009-09-11 10:50 pm
        • 2009-09-12 6:26 pm
      • 2009-09-12 1:47 am
    • 2009-09-11 4:56 pm
    • 2009-09-11 5:33 pm
    • 2009-09-12 1:44 am
      • 2009-09-12 7:02 pm
  2. 2009-09-11 3:27 pm
    • 2009-09-11 4:12 pm
      • 2009-09-11 4:52 pm
        • 2009-09-11 5:04 pm
        • 2009-09-12 2:09 am
          • 2009-09-14 2:28 pm
          • 2009-09-15 2:06 pm
    • 2009-09-11 4:30 pm
      • 2009-09-11 4:59 pm
      • 2009-09-11 5:05 pm
      • 2009-09-12 12:47 am
    • 2009-09-12 9:44 am
  3. 2009-09-11 4:06 pm
  4. 2009-09-11 4:14 pm
    • 2009-09-11 5:27 pm
      • 2009-09-11 10:05 pm
  5. 2009-09-11 4:34 pm
    • 2009-09-11 5:04 pm
  6. 2009-09-11 5:14 pm
    • 2009-09-11 5:28 pm
      • 2009-09-11 5:50 pm
      • 2009-09-11 5:50 pm
        • 2009-09-11 6:15 pm
          • 2009-09-11 6:22 pm
        • 2009-09-12 2:45 am
          • 2009-09-12 10:23 am
  7. 2009-09-11 5:50 pm
    • 2009-09-12 5:32 pm
  8. 2009-09-11 10:20 pm
    • 2009-09-12 12:50 am
    • 2009-09-12 1:57 pm
  9. 2009-09-12 12:50 am
  10. 2009-09-12 10:27 am
    • 2009-09-12 11:18 am
    • 2009-09-12 7:32 pm
  11. 2009-09-12 10:34 am
  12. 2009-09-12 11:45 am