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 useFoundation
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
orAtomicBoolean
go straight for std::atomic<> sinceobjc_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.