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:

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 .
So, what, exactly, does GCD do? Siracusa put it like this:
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:



0 