返回信息流刚看了下官方文档,人家明明白白的说了atomic与线程安全并非同义词,但是尼玛网上各种说什么线程安全的。
求解释使用atomic合成属性时不能自己实现该属性的setter和getter方法外,它和nonatomic到底啥区别?
主要是没怎么用过这个关键词,实在不清楚它的适用范围。。
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #20103同步于 2015/2/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
[我真是醉了]求解释atomic与nonatomic的区别
swkj
2015/2/6镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
“Note: Property atomicity is not synonymous with an object’s thread safety.
Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.
This example is quite simple, but the problem of thread safety becomes much more complex when considered across a network of related objects. Thread safety is covered in more detail in Concurrency Programming Guide.”
官方文档的解释
【 在 sollian 的大作中提到: 】
: 我的理解,原子变量是线程安全的
【 在 swkj 的大作中提到: 】
: “Note: Property atomicity is not synonymous with an object’s thread safety.
: Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.
: This example is quite simple, but the problem of thread safety becomes much more complex when considered across a network of related objects. Thread safety is covered in more detail in Concurrency Programming Guide.”
: ...................
对单个原子变量的操作本身是没问题的。但是对多个变量的操作,当然要考虑线程安全了····
它说的是对多个原子变量的操作
【 在 swkj 的大作中提到: 】
: “Note: Property atomicity is not synonymous with an object’s thread safety.
: Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.
: This example is quite simple, but the problem of thread safety becomes much more complex when considered across a network of related objects. Thread safety is covered in more detail in Concurrency Programming Guide.”
: ...................
关键词 原子操作
什么是原子操作。。一个线程对该属性进行读取或写入操作视为原子操作 不可被打断 不可被插入。。。假设此时来了另一个线程 操作同一个属性 那个线程操作是被阻止的
一种线程保护技术,基本上来讲,是防止在写未完成的时候被另外一个线程读取,造成数据错误。而这种机制是耗费系统资源的,所以在移动设备上,如果没有使用多线程间的通讯编程,那么请用nonatomic
简单的说
如果你把某个属性修饰了 atomic 则告诉编译器 对这个属性进行的操作都是原子操作 不会因为线程的问题 在操作一半的时候被打断(简单的说 self.title = @"北邮人论坛" 这么简单的一行代码 也是有耗时的。。如果你不修饰原子操作,你写了nonatomic。。有可能你在一个线程写入 “北邮人”的时候 突然被另外一个打断了。。你的这个title属性就乱了。。。。)(这个例子略傻逼,这么说是不对,不要深究。。。就是一种类比,形容一下 什么叫 非原子操作 会被线程打乱,楼上有人说的 一口喝完一杯酒 和分10口喝完一杯酒 是一个意思。。。你分10口喝 喝一半有可能剩下的酒被抢走了)
但是 这样编译器会很累。。。多了就会慢。。所以 一般情况大家都 nonatomic
你要非要理解 理解为一种类似线程锁的东西。。
这种东西很耗时 耗资源。。。所以如果不必要 就不要用
至于适用范围
这个属性会不会同时 在2个线程里被调用?会?用atomic
不会? nonatomic
synthesize帮你自动实现的accessor方法,会对atomic property做多线程保护; 如果是自定义的accessor,跟nonatomic没有什么不同。
再多说一下,atomic property的accessor是私有的,你就不要自己去实现accessor了。 如果你只是实现了atomic property的accessor中的一种,例如一个readwrite只实现了getter,那么编译会警告,因为你不能把一个atomic property的synthesized accessor和一个自定义的accessor结对。 如果getter和setter你都自定义了,那么编译器也就不会帮你做synthesized,需要自己为propert synthesize一个实例变量,至此atomic跟nonatomic也没区别了。总之,想用atomic就不要去自定义getter或setter了
回答LZ的问题: synthesized accessor会对atomic property背后的实例变量做多线程保护,所以你要是不通过accessor使用实例变量,那当然要自己去处理多线程安全了
多谢版主,明白了~
【 在 apocalypse 的大作中提到: 】
: 关键词 原子操作
: 什么是原子操作。。一个线程对该属性进行读取或写入操作视为原子操作 不可被打断 不可被插入。。。假设此时来了另一个线程 操作同一个属性 那个线程操作是被阻止的
: 一种线程保护技术,基本上来讲,是防止在写未完成的时候被另外一个线程读取,造成数据错误。而这种机制是耗费系统资源的,所以在移动设备上,如果没有使用多线程间的通讯编程,那么请用nonatomic
: ...................