Atomic: The default is the property of the variable, this property is to ensure that in the case of multi-threaded, the compiler will automatically generate some mutually exclusive lock code, to avoid the variable read and write out of sync problem.
Nonatomic: If the object does not need to consider multithreading, this property causes the compiler to generate less mutex code, which can improve efficiency.
Atomic means Setter/getter This function, is a primitive operation, if there are multiple threads calling the setter at the same time, there will not be a thread after executing the setter all the statements, the other thread began to perform setter condition, Equivalent to the function of the tail and the same lock, can guarantee the integrity of the data. Nonatamic does not guarantee the setter/getter of the original line, so you may be able to take something incomplete. Therefore, in the multi-threaded environment of the atomic operation is very necessary, the dog may cause the wrong result.
For example, the setter function changes two member variables, if you use Nonatomic, the getter may be taken to change only one of the variables when the state, so that the things can be a problem, is incomplete. Of course, if you do not need multi-threading support, nonatomic is enough, because it does not involve the operation of the thread lock, so it performs relatively faster.
For example, add a atomic example:
{Lock}
if (Proprity!=newvalue) {
[Property release];
property = [NewValue retain];
}
{Unlock}
Therefore, atomic will be in the multi-threaded set value when the lock, the middle of the execution layer is in a protected state, atomic is the OC used a thread protection technology, basically, is to prevent the writing is not completed when the other thread is read, resulting in data errors. And this mechanism is a waste of system resources, so on the iphone this small device, if not using the communication programming between multithreading, then Nonatomic is a very good choice!
IOS Development Atomic and nonatomic differences