Atomic vs nonatomic properties
2012-09-09 08:49
169 查看
Atomic vs
nonatomic properties
up vote356down votefavorite 189 | What do atomicand nonatomicmean in property declarations? @property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName; What is the functional difference between these 3? objective-c multithreading properties atomic
| ||||
|
9 Answers
activeoldestvotesup vote454down voteaccepted | 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). 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. 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. In nonatomic, no such guarantees are made. Thus, nonatomicis considerably faster than "atomic". 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. |
相关文章推荐
- atomic vs. nonatomic
- [译]Atomic VS. Non-Atomic 操作
- Direct vs non-direct ByteBuffer
- VS 2008 + .NET 3.5 - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- VS2013报错 error MSB8031解决方法 今天跑了一个mfc的程序,发现报错:error MSB8031: Building an MFC project for a non-U
- 解决 VS2013 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated
- “Build action” settings in VS.NET project properties and what do they do?
- iOS关键字strong/weak/assign/retain/nonatomic/atomic
- objective-C: nonatomic retain copy assgin 等属性详解
- #151 – Dependency Properties Remember Non-Coerced Values(依赖属性强制转换之前的值会被记住)
- nonatomic retain copy assign区别
- Non-Field Validator Vs Field-Validator--字段校验和非字段校验
- java direct buffer VS non-direct buffer
- c3p0.properties .vs. c3p0-config.xml
- VSCode配置文件“.vscode/c_cpp_properties.json”不断被覆盖的原因及解决方法
- login shell vs non-login shell
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- Properties vs instance variables
- Wrapper vs Bare (Non-wrapper)
- iOS关键字strong/weak/assign/retain/nonatomic/atomic