teubanks
teubanks

Reputation: 710

NSCondition vs semaphores, what's more Objective C-ish?

I'm pretty sure I understand the difference between NSCondition and semaphores (from the c api). Which one is more commonly used by Apple developers, and more particularly, iPhone developers?

Upvotes: 1

Views: 1792

Answers (2)

Ultrakorne
Ultrakorne

Reputation: 1303

dispatch_semaphore for sure

an objc way to syncronize concurrent access to a resource is to avoid locks or @syncronize if possibile and use queue, serial or concurrent (ios5 >)

if multiple threads need to access a shared resource they can dispatch to a common serial queue. By accessing the resource in this way we avoid concurrent access without kernel traps.

a concurrent queue can be used (i think in a wwdc11 video this is shown) for example when multiple threads can access a resource for reading at the same time, but we need a sync point for writing. This can be achieved easly using barriers: the thread that writes will wait until all readers are done, will write the resource exclusively on the concurrent queue and after that again, concurrents readers can access the resource.

i think this is a great resource https://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091 for doing multithreading in the objc way

Upvotes: 3

Catfish_Man
Catfish_Man

Reputation: 41821

In my experience, "neither". For whatever reason, semaphores just aren't commonly used enough for a popularity contest to matter much.

I'm a big fan of dispatch_semaphore though. Very clever implementation.

Upvotes: 7

Related Questions