Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
From the author of Addition or Replacement?

Addition or Replacement?

You can use small bits of GCD in existing applications very easily. The dispatch_async() function lets you add a block to an existing work queue (including one of the ones create by default) to run in the background. This is a cheap alternative to spawning a worker thread, and because it's so cheap you may consider doing it in some places where you were previously working synchronously.

You can also use GCD from top to bottom in new code. Cocoa already uses it for handling the run loop in 10.6, and you can do the same thing if you want to write event-driven code. You'd typically write this code on OS X by having a loop that called kevent() and then handled these events. With GCD, you can use libdispatch for this event handling, putting all of your event handles in separate functions (or blocks) and just registering them with the event sources and queues when the program starts.

GCD is probably overhyped, but that doesn't mean that it's not a nice addition to OS X, and now FreeBSD, development. It doesn't remove all of the difficulties of writing multithreaded code, but it does implement a lot of things that you'd probably want to implement yourself if it didn't exist.

  • + Share This
  • 🔖 Save To Your Account