Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "atomic" a default @property qualifier in Objective C when I find myself using nonatomic 100% of the time?

Tags:

objective-c

In my few years as an iOS developer I don't think I've ever used atomic on a property. If I can see potential race conditions or data integrity issues due to threading, using atomic on a @property wouldn't ever help. I use conventional transaction/unit-of-work thread safety techniques (using mechanisms locks, semaphores or whatever).

Does anyone have (or know of) any practical examples of where atomic is used? (I'd love to see some actual/practical code examples)

After writing nonatomic for maybe the billionth time, I'm also wondering why Apple have decided to make atomic the default.

like image 266
CVertex Avatar asked Mar 02 '11 13:03

CVertex


People also ask

What does @property do in Objective C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it.

What does Nonatomic mean in Objective C?

Nonatomic means multiple thread access the variable (dynamic type). Nonatomic is thread unsafe.

What is atomic and Nonatomic in Objective C?

In Objective-C the implementation of an atomic property allows properties to be safely read and written from different threads. For nonatomic properties, the underlying pointer of a read value could be released when a new value is being written at the same time.

What is atomic Nonatomic?

Atomic means only one thread accesses the variable (static type). Atomic is thread-safe, but it is slow. Nonatomic means multiple threads access the variable (dynamic type). Nonatomic is thread-unsafe, but it is fast. Follow this answer to receive notifications.


2 Answers

As for the first problem you're having, maybe it's because

Although “atomic” means that access to the property is thread-safe, simply making all the properties in your class atomic does not mean that your class or more generally your object graph is “thread safe”—thread safety cannot be expressed at the level of individual accessor methods.

As for why apple makes it atomic by default, I don't think there is any good reason, it was simply a bad design decision. Guys at the WWDC sessions repeatedly encouraged people to use nonatomic wherever you can to eliminate the performance impact.

like image 150
Tomas Vana Avatar answered Oct 10 '22 19:10

Tomas Vana


It's worth noting that under garbage collection, almost all synthesized accessors are inherently atomic—there would be no difference between the atomic and nonatomic version, since the simple assignment of a pointer is all that's required in both cases. Since you really can't make a nonatomic synthesized accessor under garbage collection, they may have decided that it made more sense to just have things atomic by default.

I don't have any evidence that this was the reasoning behind the decision, but it makes sense to me.

(In case you're curious, there are still cases under garbage collection where simple assignment is nonatomic—this happens when the value is larger than the word size of the process. In practice, that only happens with structs.)

Edit: Added sources

More information on the atomicity of synthesized properties under garbage collection can be found in The Objective-C Programming Language -> Declared Properties -> Performance and Threading, where it says "In a garbage collected environment, most synthesized methods are atomic without incurring this overhead." The inherent atomicity is mentioned more explicitly in the 2008 WWDC session 420 "Using Garbage Collection with Objective-C", around the 29 minute mark.

like image 27
BJ Homer Avatar answered Oct 10 '22 19:10

BJ Homer