9

I am wondering: should all properties in iPhone development be nonatomic? If so, why?

jscs
  • 62,161
  • 12
  • 145
  • 186
nyanev
  • 10,527
  • 6
  • 42
  • 76
  • 5
    Possible duplicate of [Atomic vs nonatomic properties](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties) which has an excellent answer from bbum, a known expert on this, as well as interesting discussion on most of the other answers. – jscs May 24 '11 at 19:46

4 Answers4

9

From The Objective-C Programming Language, obligatory guide:


Atomicity

You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.)

nonatomic
Specifies that accessors are nonatomic. By default, accessors are atomic.

Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. For more details, see “Performance and Threading.”

If you specify retain or copy and do not specify nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly.

sidyll
  • 51,853
  • 11
  • 92
  • 142
6

Properties can either be atomic or nonatomic.

If you declare a nonatomic property then writing to it will be much faster but it will not be thread-safe.

If you declare an atomic property then the writing will be thread-safe but it will be much slower (since it has to synchronize it so no one else touches it)

arclight
  • 5,185
  • 1
  • 19
  • 26
  • 5
    Not really, just writing atomic doesn't make sure that the property is threadsafe! It only makes sure that the property is written/read atomically, which means that when one thread is writing the property and the other is reading it, the reading thread doesn't read garbage as result of the other thread replacing the value in the meantime. – JustSid May 24 '11 at 19:41
  • Thread safety must be done on a higher level than on the getter/setter level. – JustSid May 24 '11 at 19:42
1

Performance penalty is not big deal but this is when you want to write code quick “writable atomic property 'name' cannot pair a synthesized setter with a user defined getter” , so it is better to use nonatomic , because later when you want to override getter it has problem with synthesized setter that you normally do not have to overwrite.

Renetik
  • 4,282
  • 37
  • 50
1

By default properties are atomic unless you use declare otherwise with the nonatomic keyword. This ensures sure multiple threads do not access the property while you're getting or setting its value (although it doesn't guarantee thread safety). There's a performance penalty associated with this behavior. It doesn't matter as much on the desktop, but on the iPhone it's a good idea to use nonatomic since your processor power is more limited, at least on classes you know are only going to be used on the main thread.

See also this question.

Community
  • 1
  • 1
Marc Charbonneau
  • 40,083
  • 3
  • 73
  • 82