Tuesday, May 12, 2015

Using Atomics to Get Rid of @synchronized

Bruno de Carvalho (tweet, comments):

This post talks about the use of OS low level atomic functions (compare-and-swap, fetch-and-increment) to achieve both wait and lock free, thread-safe concurrent algorithms in your iOS/OSX apps.

It introduces a few classes that abstract away the gory details of some of the atomic functions made available by libkern/OSAtomic.h into conceptually simpler components that use Foundation types.


While I’ve found plenty of use cases for atomic integers and booleans, references are something that I’ve rarely ever needed. I did come across one interesting usage for it recently.


If you’re looking to extract that extra bit of performance, instead of using AtomicInteger or AtomicBoolean go straight for std::atomic<> since objc_msgSend() — calling methods on classes — has a non-neglectable impact. Just make sure that when you do so, your code remains readable and self-explanatory.

Comments RSS · Twitter

Leave a Comment