Multithreading Brings Joy

I’ve been listening to Cocoa oriented podcasts and subscribing to RSS feeds in an attempt to get my knowledge base up to snuff. While listening to “Late Night Cocoa” I was happy to find that the most recent episode featured multithreading and the guest was the head Mac guy over at Google (aka guy who knows what he’s talking about). It was great! Very informative and it gave my wife many excuses to make nerd jokes at my expense.

At any rate, he talked about a pair of classes that remove 90% of the pain of multithreading. NSOperation and NSOperationQueue. Using those, I was able to replace all of my multithreading code in Phorg and now it runs faster, automatically scales based on system performance, and I don’t have to worry so much about locking and synchronizing. Look into those classes if you have any plans to delve into multithreading on the Mac. So worth it!

I’ve been able to add a bunch of multithreading to ToeTag today as a result of using these classes. Everything is a little faster from loading WAD files, to generating mipmaps, to loading MAPs, etc. It’s amazing how once you have multithreading at your disposal how you can suddenly see spots in your code that could benefit.

2 Responses to “Multithreading Brings Joy”

  1. nevyn Says:

    I’ve been coding for a few years, but not that much multithreaded code, so I always find it difficult to make logical designs that encapsulate that threading. More than two threads + transforming non-threadsafe code into what you *hope* is threadsafe = headache… But I agree with Mark: NSOperation really helps that design process. I had a horrible gc interaction bug, but after working around that, it works great.

  2. Warren Says:

    Sure. To be fair, the multithreading that I’ve added so far is stuff that sort of fell into my lap. I didn’t plan for it to one day become multithreaded. Most if it was happy accidents (“Hey, this routine doesn’t depend on anything and I’m calling it once for every texture. Score!“).

    But armed with these classes I’ll be able to plan for it in the future and that should be pretty great.

Leave a Reply