I know there are answers on atomic vs. non-atomic answers, but they mostly seem to be fairly old (2011 and earlier), so I'm hoping for updated advice. My understanding is that non-atomic properties are faster but not thread safe. Does this mean that any property that might be accessed from multiple threads at the same time should always be atomic? Are there conditions that can make it ok to make it non-atomic? What other concerns are there in determining whether to make a property atomic or nonatomic?
-
Nowadays always use "Atomic". It's that simple. "Non-atomic" is just a relic from the past -- you may as well ask "Are there situations where I should not use ARC?" – Fattie Nov 18 '15 at 20:24
-
*... in determining whether ..." there are no determinations. Atomic is the default, forget the whole issue. It's very likely they'll just remove the non-atomic concept in the future. – Fattie Nov 18 '15 at 20:25
-
I don't know of any reason to think that the answer to this has changed. The replies you've already gotten here repeat the information already provided in earlier questions. If you have a clear and demonstrable reason to believe that this topic _requires_ an updated response, please [edit] your post to explain that reason, and ping me to reopen. – jscs Nov 18 '15 at 20:42
4 Answers
Declaring a property atomic
makes compiler generate additional code that prevents concurrent access to the property. This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. Compared to setting or getting a primitive value or a pointer, locking and unlocking a semaphore is expensive (although it is usually negligible if you consider the overall flow of your app).
Since most of your classes under iOS, especially the ones related to UI, will be used in a single-threaded environment, it is safe to drop atomic
(i.e. write nonatomic
, because properties are atomic
by default), even though the operation is relatively inexpensive, you do not want to pay for things that you do not need.

- 1,891
- 16
- 23
Declaring a property as atomic does not necessarily make it thread safe.
Atomic is the default and involves some extra overhead compared to nonatomic. If thread A is halfway through the getter for that property and thread B changes the value in the setter, using atomic will ensure that a viable, whole value is returned from the getter. If you use nonatomic no such guarantee is made, the extra code is not generated, and thus nonatomic is faster.
However, This does not guarantee thread safety. If thread A calls the getter and threads B and C are updating the thread with different values then thread A could get either value and there is no guarantee which one it will get.
To specifically answer your question, many scenarios allow for nonatomic properties, if not most. Although the extra overhead of using atomic is probably negligable. Simply put, are your properties read or set on different threads? If not, you most likely don't need to declare them as atomic but the extra overhead might not even be noticable. It's just that simply declaring them as atomic does not guarantee thread safety.

- 3,389
- 23
- 39
In most situations it is unimportant, whether a property is atomic or not in a multithreaded environment.
What?
In most situations it is unimportant, whether a property is atomic or not in a multithreaded environment.
The reason for this is that making a property "thread-safe" by turning atomicity on does not make your code thread-safe. To get this you need more work and this work typically implicitly ensures that the property is not accessed parallel.
Let's have an example: You have a class Person
with the properties firstName
and lastName
both of NSString*
. You simply want to add the both name separated with a space to have a full name.
NSString *fullName = [NSString stringWithFormat:@"%@ %@", person.firstName, person.lastName];
You know that other threads can update the properties while you do that. Making the properties atomic doesn't help anything. This does not help, if the last name of the persons are changed, after reading the first name, but before reading the second name. This does not help, if the names of the persons are changed after calculating the full name, because this can be invalid in the very next moment.
You have to serialize operations, not property accesses. But atomicity only serializes accesses. So it does not help, if at least one operation has more than one access. 99,999999373 % of all cases.
Forget about property atomicity. It is meaningless.

- 16,582
- 3
- 35
- 50
There are no other concerns. Yes, any property that can be accessed on multiple threads should be atomic or you can end up with unexpected results.

- 2,578
- 2
- 15
- 26