humancode.us

Why GCD?

July 31, 2014

GCD Logo

This is the first post in a series about Grand Central Dispatch.

In short: better parallelism.

In the last decade, CPU speed improvements have hit a wall. To continue to get more performance out of the same clock speed, CPU manufacturers have been including more and more cores on their dies (my MacBook Pro has 8 cores and my Mac Pro at work has 24).

CPU speed over the years

(Source)

The problem is, programs can’t take advantage of these extra cores unless it knows how to farm out its work effectively.

Parallelism deals with the problem of doling out multiple jobs to be done at the same time. This is a very hard problem in general, but GCD makes it a little easier to manage.

You may not have a need to write a massively parallel program, but you can still use GCD to make a more responsive program by not having your UI or service wait for jobs to complete. Typically, you want to toss jobs to other cores while your main program continues serving requests asynchronously. GCD makes this, and other similar tasks, easier.

Read more…

An Introduction to Grand Central Dispatch

July 28, 2014

GCD Logo

Grand Central Dispatch is an important system library introduced in OS X 10.6 and iOS 4. It provides a set of C API that makes it easy(er) to deal with parallel programming on iOS and OS X. GCD works with C, C++, Objective C, and even Swift.

When you begin exploring GCD, you discover two things right away: its API is terse, and there is hardly anything in the way of introductory materials. Wouldn’t it be nice if there were a gentle introduction to guide you through some basic patterns that you should use or avoid?

That’s what this series of posts is intended to do. I’ve been using GCD since its early days, and I have battle scars to prove it. I hope I can act as a guide to this powerful and elegant library.

Over the next few weeks, I will post several blog posts that deal with aspects of GCD to help you get started, get comfortable, and get creative with the API.

I will be tagging the posts in this series with #libdispatch.

Posts in this series:

  1. Why GCD?
  2. Using GCD Queues For Synchronization
  3. GCD Concurrent Queues
  4. GCD Target Queues
  5. Writing Thread-Safe Classes with GCD
  6. Keeping Things Straight with GCD

Block APIs are NOT harmful

July 17, 2014

Note: This is a repost of an older post that was published on my tumblr blog. Before I begin writing more posts on the subject of programming, I thought I’d practice by reposting something I’ve already written. This post has been re-edited for clarity.

I take exception to Drew Crawford’s recent post that block-based APIs introduce “harmful” surprising side effects into Objective-C, especially under ARC. Drew’s post calls attention to NSNotificationCenter in particular, but really, any block-based API is susceptible to similar kinds of pitfalls.

I would encourage you to download a copy of Drew’s example code as you read his blog post to understand the problem he’s describing.

Read more…

Moving to Jekyll

July 16, 2014

I’ve been wanting to write more blog posts on the subject of software development and code in general, and tumblr just isn’t cutting it for me, mainly because it’s so ridiculously hard to format and syntax-highlight code in the post.

I’ve been intrigued by static site generators, and I finally settled on Jekyll, hosted on GitHub Pages. Jekyll allows me to have manual control over layout and style, and allows me to write posts in glorious Markdown instead of HTML-in-a-rich-editor.

Plus, code:

@interface Hello: NSObject
- (void)hello;
@end

@implementation Hello
- (void)hello {
    NSLog(@"Hello, world!");
}
@end

Anyway, it’s also nice to get back into hacking HTML and CSS. I hope I learn a thing or two along the way.

You can still get to my old blog hosted on tumblr here. The old posts have been automatically imported into this blog (with mixed results).

Not Yet

July 9, 2014

Copyright ©2014 Dave Rahardja