For Kompressor.app, 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?
- Create an instance of SIWorkManager (usually there should only be one); by default it creates as many worker threads as CPU cores are available.
- 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.
- Tell the work manager object about the work unit (via addWorkUnit:).
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.