I am discussing whether to move on to a GCD-based template for multi-threaded accessories. I have been using lock-based user synchronization in accessors for many years, but I found some information ( Introduction to GCD ), and there seem to be advantages to the GCD oriented approach. I hope to start a dialogue here to help myself, while others are weighing the decision.
The sample looks like this:
- (id)something { __block id localSomething; dispatch_sync(queue, ^{ localSomething = [something retain]; }); return [localSomething autorelease]; } - (void)setSomething:(id)newSomething { dispatch_async(queue, ^{ if(newSomething != something) { [something release]; something = [newSomething retain]; [self updateSomethingCaches]; } }); }
On the pro side: you get the advantage of possibly non-blocking write access; Lower overhead than blocking (maybe?); safety from forgetting to unlocking before returning from critical sections of code; others?
Cons: Exception handling does not exist, so you need to code it in every block that you might need.
Is this a potentially recommended method for writing multi-threaded accessories?
Are there standard approaches for creating dispatch queues for this purpose? In other words, best practices for trading from granularity? For example, using locks, the lock for each attribute is more subtle than the lock on the entire object. With dispatch queues, I could imagine that creating one queue for all objects would create performance bottlenecks, so are object queues suitable? Obviously, the answer is highly application-specific, but there are known trade-offs with performance to help evaluate the feasibility of the approach.
Any information / understanding will be appreciated.
greg
source share