The difference between atomic and nonatomic,assign copy and retain

Source: Internet
Author: User

Atomic and nonatomic are used to determine whether the compiler-generated getter and setter are atomic operations.

  Atomic

When setting the @property property of a member variable, the default is atomic, which provides multithreading security.

In a multithreaded environment, atomic operations are necessary, or they can cause incorrect results. The addition of the Atomic,setter function will become the following:

{Lock}

F (Property!= NewValue) {

[Property release];

property = [NewValue retain];

}

{Unlock}

 Nonatomic

Prevents multiple threads, variable protection, and improved performance.

Atomic is a thread-protection technique used by OBJC, which basically prevents a data error from being read by another thread when the write is not completed. This mechanism consumes system resources, so nonatomic is a very good choice for a small device like the iphone without using multithreaded communication programming.

Indicates that the accessor is not an atomic operation, and by default, the accessor is an atomic operation. This means that, in a multithreaded environment, the parsed accessor provides a secure access to a property, a return value obtained from the accessor, or a value set by the setting can be completed at once, even if another thread is accessing it. If you do not specify nonatomic, the parsed accessor retains and automatically releases the returned value in the environment in which it manages memory, and if Nonatomic is specified, then the accessor simply returns the value.

  Assign

For the underlying data type (nsinteger,cgfloat) and C data types (int, float, double, char), and so on.

This tag explains that the setup directly assigns the value, which is also the default value. In applications that use garbage collection, if you want a property that uses assign, and this class conforms to the nscopying protocol, you need to explicitly point to the tag, rather than simply using the default value, otherwise you'll get a compile warning. This again indicates to the compiler that you really need to assign a value, even if it is copy-able.

  Retain

To release the old value for the other nsobject and its subclasses, and then retain the new value

Specifies the retain message that retain will wake the incoming value when the value is assigned. This property can only be used for objective-c object types and not for core Foundation objects. (Obviously, retain increases the reference count of the object, while the base data type or the core foundation object does not have a reference count--the translator's note).

Note: When you add an object to an array, the reference count increases the number of references to the object by +1.

 Copy

For NSString It indicates that a copy of the passed-in value is used when assigning a value. Copy work is performed by the copy method, which is valid only for the type of object that implements the Nscopying protocol. For more in-depth discussion, refer to the "Replication" section.

  Copy and retain:

Copy is actually creating a same object, and retain is not:

1. such as a NSString object, the address is 0x1111, the content is @ "STR", Copy to another nsstring, the address is 0x2222, the content is the same.

2. The new object retain is 1, the old object has not changed retain to another nsstring, the address is the same (set up a pointer, the copy of the pointer), the content is the same, the retain value of this object is +1.

  Summarize:

Retain is a pointer copy and copy is a copy of the content.

Assign and retain:

1. Contact with C, then suppose you use malloc to allocate a piece of memory, and the address assigned to the pointer A, and then you want pointer b to share the memory, and then you assign a value to (assign) B. When a and B point to the same memory, when a no longer need this block of memory, can you directly release it? The answer is no, because a does not know whether B is still using this block of memory, if A is released, then B in the use of this memory will cause the program crash off.

2. Understand the problem of the assign in 1, so how to solve it? The simplest method is to use the reference count (reference counting), or the example above, where we set a reference count for that block of memory, and when the memory is allocated and assigned to a, the reference count is 1. The reference count increases to 2 when assigning a value to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.

The above two points is actually the difference between assign and retain, assign is directly assigned, which may cause problems in 1, when the data is an int, float and other native types, you can use assign. Retain as described in 2, using the reference count, retain caused the reference count plus 1, release caused the reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called, memory is reclaimed.

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.