Reputation:
I know there has been much discussion, one of which is https://stackoverflow.com/a/12969166/1724763 but the answer is not very clear.
I just want focus on specific use cases of atomic not found elsewhere (in the context of ARC)
For example, I have :
@property (nonatomic, strong) someobject;
In this case, someobject is written once in init
and readonly thereafter by multiple threads.
Do I have to make it atomic
? What exactly does the getter do? just return the pointer?
For scalars and non-objective c objects, can I just make them nonatomic
for multi-thread readwrite? I understand that on Intel processors reading and writing an aligned int is always atomic.
Also, when accessing an atomic property, should I assign to a local variable first and use the local var to improve performance?
Upvotes: 2
Views: 399
Reputation: 162712
nonatomic
is safe in this case (where this case is an object that is static throughout the app's session).
No need to do the retain
/autorelease
dance, even.
For all intents and purposes, this is equivalent in behavior to your typical sharedInstance
method. Specifically, the readonly
value is computed in a thread safe, exclusionary, manner and, once computed, the value never ever changes.
As far as performance? There is no reason to assign to a local variable until you identify a quantifiable and repeatable real world performance issue.
Upvotes: 2