2

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?

Community
  • 1
  • 1
  • Search for discussions of atomic vs. nonatomic. atomic avoids crashes when a property is accessed by multiple threads, but doesn't make your code thread safe. As a result, atomic is very rarely used. – gnasher729 Apr 02 '15 at 16:43
  • @gnasher729 Lucas asked this question in response to the atomic vs. nonatomic details. It is a valid and specific question. – bbum Apr 02 '15 at 20:16

1 Answers1

2

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.

bbum
  • 162,346
  • 23
  • 271
  • 359