IOS Development Atomic and nonatomic differences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.