tags:

views:

24

answers:

1

I'm not quite sure if I understand atomic correctly. From what I read, it says that atomic is the default for the iPhone. Now is that for properties only, or any instance variable. For example, if I have an instance variable that I am going to write my own setters/getters, and do not declare it as a property, does that make that instance variable atomic? Is the downside mainly that it is optimized for threading, which my instance variable / application might not even need? Thanks.

A: 

The atomic keyword in property declarations does double duty--it's both documentation and an instruction for synthesized accessors. The keyword only applies to accessor methods--manually accessing ivars is essentially the same as accessing a C struct, and is never atomic.

You can still write your own accessors if you use property declaration syntax, but if you do, you should adhere to the declaration (if you don't declare it nonatomic, you should manually implement atomicity). If you @synthesize your properties, they will follow your declaration automatically.

The downside of atomic properties is that they use locking, which is quite expensive--if the property won't be accessed by multiple threads, you should always declare it nonatomic (at least on the iPhone).

eman