Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-threaded Objective-C accessors: GCD vs locks
    text
    copied!<p>I'm debating whether or not to move to a GCD-based pattern for multi-threaded accessors. I've been using custom lock-based synchronization in accessors for years, but I've found some info (<a href="http://www.mikeash.com/pyblog/friday-qa-2009-08-28-intro-to-grand-central-dispatch-part-i-basics-and-dispatch-queues.html">Intro to GCD</a>) and there seems to be pros of a GCD-based approach. I'm hoping to start a dialog here to help myself and others weigh the decision. </p> <p>The pattern looks like:</p> <pre><code>- (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]; } }); } </code></pre> <p>On the pro side: you get the benefit of, possibly, non-blocking write access; lower overhead than locks (maybe?); safety from forgetting to unlock before returning from critical code sections; others?</p> <p>Cons: Exception handling is non-existent so you have to code this into every block in which you might need it.</p> <p>Is this pattern potentially the recommended method of writing multithreaded accessors? </p> <p>Are there standard approaches for creating dispatch queues for this purpose? In other words, best practices for trading off granularity? With locks, for example, locking on each attribute is more fine grain than locking on the entire object. With dispatch queues, I could imagine that creation of a single queue for all objects would create performance bottlenecks, so are per-object queues appropriate? Obviously , the answer is highly dependent on the specific application, but are there known performance tradeoffs to help gauge the feasibility of the approach. </p> <p>Any information / insight would be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload