objective c - Does dispatch_sync have a conceptual performance advantage over a lock? -


in objective-c, there (at least) 2 approaches synchronizing concurrent accesses shared resource. older lock-based approach , newer approach grand central dispatch (gcd), latter 1 using dispatch_sync dispatch accesses shared queue.

in concurrency programming guide, section eliminating lock-based code, stated "the use of locks comes @ cost. in non-contested case, there performance penalty associated taking lock."

is valid argument gcd approach?

i think it's not following reason:

a queue must have list of queued tasks do. 1 ore more threads can add tasks list via dispatch_sync , 1 or more worker threads need remove elements list in order execute tasks. must guarded lock. lock needs taken there well.

please tell me if there other way how queues can without lock i'm not aware of.

update: further on in guide, implied there i'm not aware of: "queueing task not require trapping kernel acquire mutex."

how work?

there exist lock free queuing implementations. 1 reason pooh-poohed platform specific, since rely on processors atomic operations (like increment, decrement, compare-and-swap, etc) , exact implementation of vary 1 cpu architecture another. since apple both os , hardware vendor, criticism far less of issue apple platforms.

the implication documentation gcd queue management uses 1 of these lock-free queues achieve thread safety without trapping kernel.

for more information 1 possible macos/ios lock-free queue implementation, read here these functions:

void  osatomicenqueue( osqueuehead *__list, void *__new, size_t __offset); void* osatomicdequeue( osqueuehead *__list, size_t __offset); 

it's worth mentioning here gcd has been (mostly) open-sourced, if you're curious implementation of it's queues, go forth , use source, luke.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -