As many of you may know, Apple has released a new concurrency API with Snow Leopard, called Grand Central Dispatch (GCD). You may also know that, contrary to some of the hype about GCD being a totally new facility for programmers, there was already a similar concurrency abstraction available to Cocoa programmers prior to Snow Leopard, called Operation Queues (OQ), using the NSOperation and NSOperationQueue classes. What's more, the OQ API has been updated in Snow Leopard to accept blocks as operations.
I've been reading the documentation for GCD to see what is different about it compared to OQ. Also I am wondering what advantages there are, if any, to using GCD instead of OQ.
Let me start off by listing some differences I'm already aware of.
I guess what I'm really wondering is, assuming I'm not concerned about the first two points listed above, is there any reason to prefer GCD over OQ? Is there any performance benefit of GCD over OQ? I gather from some of the OQ documentation that in Snow Leopard OQs may in fact be using GCD under the hood now anyway, which would mean that it has the same advantages as far as global scheduling of concurrent tasks. However I haven't seen that explicitly stated. I read through Apple's new Concurrency Programming Guide, which covers both APIs, but it doesn't really give any guidance about why you'd choose one over the other. Thanks for ideas.
I've been reading the documentation for GCD to see what is different about it compared to OQ. Also I am wondering what advantages there are, if any, to using GCD instead of OQ.
Let me start off by listing some differences I'm already aware of.
- OQ is a Cocoa API, whereas GCD is a C API, so obviously if the concurrent code you're writing is not Cocoa code, then only the GCD API will be useful to you.
- GCD has a facility called Dispatch Sources, to dispatch new tasks every time certain events occur, such as timers firing, file descriptor events, process events, etc. OQ has no equivalent facility.
- OQ has a more powerful and flexible way of specifying dependencies among tasks compared to GCD. With OQ you can specify that an operation has any number of other operations as dependencies, which means the dependency operations must complete before this operation can proceed. With GCD, you only have the choice of putting tasks on a serial or concurrent dispatch queue. Let's say I have tasks A, B, C and D, and tasks A, B and C must complete before task D can proceed. With OQs, I could add A, B and C as dependencies of D, and then add all the operations to a queue. The queue would then know it was free to run A, B and C concurrently, but D has to wait until A, B, and C are done. It's not as straightforward to express the same thing with GCD.
I guess what I'm really wondering is, assuming I'm not concerned about the first two points listed above, is there any reason to prefer GCD over OQ? Is there any performance benefit of GCD over OQ? I gather from some of the OQ documentation that in Snow Leopard OQs may in fact be using GCD under the hood now anyway, which would mean that it has the same advantages as far as global scheduling of concurrent tasks. However I haven't seen that explicitly stated. I read through Apple's new Concurrency Programming Guide, which covers both APIs, but it doesn't really give any guidance about why you'd choose one over the other. Thanks for ideas.