Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I ever use nonatomic in a property? [duplicate]

Tags:

objective-c

Possible Duplicate:
Objective-C properties: atomic vs nonatomic

By default all properties in Objective-C are atomic. If I need nonatomic I have to declare it. But I wonder why should I ever use nonatomic? Even if my applications are not multi threaded, atomic seems like the way to do it. What are the advantages of nonatomic?

like image 957
TalkingCode Avatar asked Oct 31 '10 15:10

TalkingCode


People also ask

What is Nonatomic property?

Non atomic properties has no guarantee regarding the returned value . It can be the correct value, a partially written value or even some garbage value. As most things that are not safe — this comes with enhanced speed of accessing this properties.

What is the difference between atomic and nonatomic properties?

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.

What is the difference between atomic and nonatomic properties Swift?

Swift has no such specifier. 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 does Nonatomic mean?

a : not relating to, concerned with, or composed of atoms Gerald Cleaver, professor and graduate program director in Baylorʼs department of physics, will present "Life on the Landscape," which will consider the place of the Earthʼs universe and the possibility of nonatomic-based (intelligent) life forms outside of it. ...


1 Answers

A short answer is performance. If you declare properties as atomic, the synthesized accessors will use locks to ensure that values are fully retrieved and set. If you don't need this, e.g., your application is single-threaded, you're incurring a performance penalty for those locks without getting a benefit.

like image 65
James Huddleston Avatar answered Nov 16 '22 01:11

James Huddleston