本文介绍了原子属性和非原子属性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

atomicnonatomic 在属性声明中是什么意思?

What do atomic and nonatomic mean in property declarations?

@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;
@property(retain) UITextField *userName;

这三者的操作区别是什么?

What is the operational difference between these three?

推荐答案

最后两个相同;"atomic" 是默认行为( -- atomic在最近版本的 llvm/clang 中被添加为关键字).

The last two are identical; "atomic" is the default behavior ( -- atomic was added as a keyword in recent versions of llvm/clang).

假设您正在@synthesizing 方法实现,原子与非原子会更改生成的代码.如果您正在编写自己的 setter/getter,原子/非原子/保留/分配/复制仅仅是建议性的.(注意:@synthesize 现在是最新版本的 LLVM 中的默认行为.也不需要声明实例变量;它们也会自动合成,并且会在它们的名称前加上 _以防止意外直接访问).

Assuming that you are @synthesizing the method implementations, atomic vs. non-atomic changes the generated code. If you are writing your own setter/getters, atomic/nonatomic/retain/assign/copy are merely advisory. (Note: @synthesize is now the default behavior in recent versions of LLVM. There is also no need to declare instance variables; they will be synthesized automatically, too, and will have an _ prepended to their name to prevent accidental direct access).

使用atomic",合成的setter/getter 将确保整个 值总是从getter 返回或由setter 设置,而不管任何其他线程上的setter 活动如何.也就是说,如果线程 A 在 getter 的中间,而线程 B 调用 setter,则一个实际可行的值——一个自动释放的对象,最有可能——将返回给 A 中的调用者.

With "atomic", the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A.

nonatomic 中,没有做出这样的保证.因此,nonatomic 比atomic"快得多.

In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than "atomic".

原子"做的是对线程安全做出任何保证.如果线程 A 在调用 getter 的同时,线程 B 和 C 使用不同的值调用 setter,线程 A 可能会获得返回的三个值中的任何一个——调用任何 setter 之前的值或传递给 setter 的值之一在 B 和 C 中.同样,对象可能以 B 或 C 中的值结束,无从判断.

What "atomic" does not do is make any guarantees about thread safety. If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned -- the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell.

确保数据完整性——多线程编程的主要挑战之一——是通过其他方式实现的.

Ensuring data integrity -- one of the primary challenges of multi-threaded programming -- is achieved by other means.

补充:

atomicity 单个属性的原子性在多个依赖属性同时起作用时也不能保证线程安全.

atomicity of a single property also cannot guarantee thread safety when multiple dependent properties are in play.

考虑:

 @property(atomic, copy) NSString *firstName;
 @property(atomic, copy) NSString *lastName;
 @property(readonly, atomic, copy) NSString *fullName;

在这种情况下,线程 A 可以通过调用 setFirstName: 然后调用 setLastName: 来重命名对象.同时,线程 B 可能会在线程 A 的两次调用之间调用 fullName,并且会收到新的名字和旧的姓氏.

In this case, thread A could be renaming the object by calling setFirstName: and then calling setLastName:. In the meantime, thread B may call fullName in between thread A's two calls and will receive the new first name coupled with the old last name.

要解决这个问题,您需要一个事务模型.IE.某种其他类型的同步和/或排除允许在更新依赖属性时排除对 fullName 的访问.

To address this, you need a transactional model. I.e. some other kind of synchronization and/or exclusion that allows one to exclude access to fullName while the dependent properties are being updated.

这篇关于原子属性和非原子属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:50