Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

ios - 原子属性和非原子属性有什么区别?(What's the difference between the atomic and nonatomic attributes?)

What do atomic and nonatomic mean in property declarations?

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

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

What is the operational difference between these three?

(这三个之间的操作区别是什么?)

  ask by Alex Wayne translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The last two are identical;

(最后两个是相同的;)

"atomic" is the default behavior ( note that it is not actually a keyword; it is specified only by the absence of nonatomic -- atomic was added as a keyword in recent versions of llvm/clang).

(“ atomic”是默认行为( 请注意,它实际上不是关键字;仅通过不存在 nonatomic atomic 来指定 -在最近的llvm / clang版本中将atomic作为关键字添加)。)

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.

(如果您正在编写自己的setter / getter,则原子/非原子/保留/分配/复制仅是建议性的。)

(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).

((注意:@synthesize现在是LLVM的最新版本中的默认行为。也无需声明实例变量;它们也将自动合成,并且名称前会带有_ ,以防止意外的直接访问。))

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.

(使用“ atomic”,合成的setter / getter将确保始终从getter返回或由setter设置整个值,而不管任何其他线程上的setter活动如何。)

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.

(也就是说,如果线程A在getter的中间,而线程B在调用setter时,实际的可行值(很可能是自动释放的对象)将返回给A中的调用者。)

In nonatomic , no such guarantees are made.

(在nonatomic ,不会做出任何此类保证。)

Thus, nonatomic is considerably faster than "atomic".

(因此, nonatomic比“原子”要快得多。)

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.

(如果线程A在调用线程B的同时同时调用线程B和C调用具有不同值的setter,则线程A可能会获得返回的三个值中的任何一个-在调用任何setter之前的值之一,或者将其中一个值传递给setter同样,对象可能以B或C中的值结尾,无法分辨。)

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

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

Adding to this:

(添加到此:)

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

(当多个从属属性在起作用时,单个属性的atomicity性也不能保证线程安全。)

Consider:

(考虑:)

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

In this case, thread A could be renaming the object by calling setFirstName: and then calling setLastName: .

(在这种情况下,线程A可以通过调用setFirstName: setLastName:然后调用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.

(同时,线程B可能在线程A的两次调用之间调用fullName ,并将接收新的名字和旧的名字。)

To address this, you need a transactional model .

(为了解决这个问题,您需要一个事务模型 。)

Ie some other kind of synchronization and/or exclusion that allows one to exclude access to fullName while the dependent properties are being updated.

(即某种其他类型的同步和/或排除,允许在更新从属属性时排除对fullName访问。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...