iOS Development multithreaded article---atomic nonatomic differences

Source: Internet
Author: User

SummaryThe atomic and nonatomic differences are used to determine whether the getter and setter generated by the compiler are atomic operations. Atomic provides multithreading security, is to describe whether the variable supports multi-threaded synchronous access, if you choose atomic then that is, the system will automatically create lock locks, locking variables. Nonatomic prohibit multi-threading, variable protection, improve performance.
    • Atomic: The default is the property, this property is to ensure that the program in the multi-threaded case, the compiler will automatically generate some mutex lock code, to avoid the variable read and write synchronization problems.
      Nonatomic: If the object does not need to consider multithreading, add this property, which will allow the compiler to generate a few mutually exclusive lock code, can improve efficiency.

    • Atomic means setter/getter This function, which is a primitive operation. If more than one thread calls the setter at the same time, one thread does not run out of all the setter statements, and the other thread starts to perform the setter condition, which is equivalent to a lock on the end of the function, which guarantees the integrity of the data. Nonatomic does not guarantee the setter/getter of the original line, so you may be able to take something incomplete. Therefore, in multi-threaded environment atomic operation is very necessary, otherwise it may cause incorrect results.

    • For example, the setter function changes two member variables, if you use Nonatomic, 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, with Nonatomic is enough, because it does not involve the operation of the lock, so it is relatively faster execution rate.

    • Here is an example of a atomic on the online section of the catalogue:

    • {Lock}
      if (Property! = newvalue) {
      [Property release];
      property = [NewValue retain];
                                      }
                      

    • It can be seen that with atomic will be in the multi-threaded setting value of 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. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.

iOS Development multithreaded article---atomic 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.