When we started on our first Leopard-only software, we were excited to to use NSOperationQueue, as it promised to greatly ease the pain of multithreading. And it lived up to its promise well, right up to the point where we discovered that it was fatally broken.
(And I mean fatally broken. It spontaneously crashes. There’s no workaround and it’s unlikely that it will get fixed in 10.5.)
By that time we had already written a ton of code that revolved around NSOperationQueue. If it weren’t unusably broken, it would have been a great way to work. So we decided to simply write our own replacement instead. Since NSOperationQueue is a nice API, and since we all hate crashy Mac OS X software, we decided to release it to the world so that we can all benefit.
You can download RAOperationQueue here.
Note that this is not a 100% drop-in replacement for NSOperationQueue. Since it was written for our code, it only does what we needed it to do, so it has some limitations. Specifically, RAOperationQueue:
- Only supports one running operation at a time in each queue. In other words, equivalent to setMaxConcurrentOperationCount:1 no matter what.
- Only supports two priority levels.
- Has no support for inter-operation dependencies.
- Has no support for “concurrent” operations which don’t need to be run on a worker thread.
- Spawns one worker thread per queue instead of sharing a pool of worker threads.
- Perhaps some others.
However it also has some significant benefits:
- 100% lockless and non-blocking in all circumstances, except that the worker thread will block when no operations are pending.
- Full control over worker threads, can potentially set custom priorities or other thread properties.
- Shouldn’t crash. If it does, you have the code and can fix the problem!
Whether you can live with the limitations is of course something only you can decide, but we’re making very good use of this code internally, and we hope that you can too.
Comments, questions, bug fixes, and other contributions are welcome by e-mail to mikeash at this domain.
RAOperationQueue is made available under a very liberal license which just requires a mention in your about box. We hope you’ll find it as useful as we have.
Update: As of Mac OS X 10.5.7, NSOperationQueue is fixed and safe to use again.