Monthly Archives: January 2007

Cocoa Worker Thread

For, I’ve written a group of Foundation classes for implementing a worker-thread paradigm in Objective-C. The general idea is you have some non-trivial amount of processing you want done and then (optionally) be notified when it’s done.

This implementation has a number of advantages compared to others:

  • Threads are created once and reused, no expensive thread creation for each work unit.
  • Non-polling (using NSConditionLock).
  • Low communication overhead (e.g. no Distributed Objects).
  • Small (although the code is spread out over 3 classes / files).

How do you use it?

  1. Create an instance of SIWorkManager (usually there should only be one); by default it creates as many worker threads as CPU cores are available.
  2. For each unit of work, create an SIWorkUnit object (which contains the target object and selector where the actual computation is done, as well as an optional didEndSelector which is called on the main-thread of your application with either the result of calling [target selector] or the originally given argument). The target and argument are retained by the work unit.
  3. Tell the work manager object about the work unit (via addWorkUnit:).
  4. Done!

If you want to wait for a batch of N work units to be done, create an NSConditionLock L, and at the end of the work selector, you do [L lock]; [L unlockWithCondition:[L condition] + 1];. After dispatching all the work units in the main thread (or wherever), you wait with [L lockWhenCondition:N]; [L unlockWithCondition:0];.

An (untested as I am only on a Linux machine at the moment) example demonstrating both simple usage as well as waiting for a batch to be finished is in BatchExample.m.

Remember to only access shared data structures (or your user-interface) in the didEndSelector. SIWorkManager also has messages for removing work on or referring to a given object. Use nil to remove all pending work units.

Here is the source code with all required files. Feedback / fixes appreciated.

Music for 2007

While coding during the last month I’ve been listening to a lot of music via Pandora (a music recommendation service / internet radio station). I’m listing some of the stand-out tracks / artists I’ve enjoyed here

  • Röyksopp – The Understanding (Norwegian, kinda groovy, kinda poppy)
  • Amos Lee – Keep It Loose, Keep It Tight (relaxed)
  • UNKLE – Reign (electronic strings + big beats)
  • Crooked Fingers – Dignity and Shame (piano with slightly odd vocals)
  • Cat Power – I Found a Reason (via V for Vendetta)
  • The Prodigy – Voodoo People (Pendulum Remix) (more aggressive and faster than the original)
  • Kasabian – Club Foot (from Tony Hawk’s Project 8 trailer)
  • Frou Frou – Details (pop-rock with nice female vocals, now part of Imogen Heap)
  • M83 – Teen Angst (from the A Scanner Darkly trailer)
  • Alpinestars – Carbon Kid (from the Test Drive Unlimited soundtrack, sounds like guest vocals from Placebo)
  • Rosie Thomas – When We Were Small (Farewell, Wedding Day, Bicycle Tricycle)
  • Ryan Star – Songs from the Eye of an Elephant (piano ballads)
  • Beazley Phillips Band – Shooting Ghosts (see above)
  • Camera Obscura – Pen and Notebook (female vocals + piano)
  • Apparat – Komponent (supposedly intelligent dance music (IDM), reminded me a lot of The Notwist)
  • Apparat & Ellen Allien – Orchestra of Bubbles (this actually is IDM, song tip: Rotary)
  • Statistics – Leave Your Name & Often Lie (alternative rock with some electronica, try A Number, Not a Name or Nobody Knows Your Name)