Question

Is there performance gain when using a serial dispatch queue (assuming high or default priority) over @synchronized blocks to serialize access to a resource. By gain, I mean any difference in the overhead due to the synchronization mechanism itself.

Was it helpful?

Solution

Certainly. There's also differences in semantics though. An @synchronized block uses a recursive, exception-safe mutex in a side-table. All of those properties lead to some additional overhead. dispatch_queue_t is extremely light weight (especially when using dispatch_*_f to avoid the Block_copy()), but is non-recursive, doesn't handle exceptions, and doesn't guarantee a particular thread.

Personally, I think recursive locks are a bad idea, and exceptions really shouldn't be caught in Cocoa apps, so @synchronized has very little appeal.

Future edit: the newer os_unfair_lock is even significantly lower overhead than either

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top