属性“非原子”是什么,意思? [英] What does the property "Nonatomic" mean?
问题描述
这段代码中的非原子是什么意思?
What does "nonatomic" mean in this code?
@property(nonatomic, retain) UITextField *theUsersName;
原子和非原子之间有什么区别?
What is the difference between atomic and nonatomic?
感谢
推荐答案
查看 Apple文档。
基本上,如果您说非原子
,并使用 @synthesize
生成访问器,然后如果多个线程尝试更改/读取属性一旦,坏会发生。您可以获取部分写入的值或过度释放/保留的对象,这很容易导致崩溃。 (这可能比原子访问器快很多。)
Basically, if you say nonatomic
, and you generate the accessors using @synthesize
, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)
如果使用默认值( atomic
;以前没有关键字,但有现在是),那么 @synthesize
d方法使用对象级锁来确保对单个属性的多次读取/写入被序列化。正如苹果文档指出,这并不意味着整个对象是线程安全的,但是单个属性的读/写是。
If you use the default (which is atomic
; there used to be no keyword for this, but there is now), then the @synthesize
d methods use an object-level lock to ensure that multiple reads/writes to a single property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the individual property reads/writes are.
当然,如果你实现你自己的访问器而不是使用 @synthesize
,我认为这些声明什么都不做,除了表达你的意图是否属性是以线程安全的方式实现。
Of course, if you implement your own accessors rather than using @synthesize
, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.
这篇关于属性“非原子”是什么,意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!