ios 为什么在 Objective C 中总是说属性是非原子的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17571338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:17:59  来源:igfitidea点击:

Why properties are always said to be made nonatomic in Objective C?

iosobjective-c

提问by dasblinkenlight

It is said that nonatomicoption will make your setter methodrun faster. I googled it but am not able to understand. Could someone tell me why?

据说该nonatomic选项将使您的setter 方法运行得更快。我用谷歌搜索但无法理解。有人能告诉我为什么吗?

回答by dasblinkenlight

Declaring a property atomicmakes compiler generate additional code that prevents concurrent access to the property. This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. Compared to setting or getting a primitive value or a pointer, locking and unlocking a semaphore is expensive (although it is usually negligible if you consider the overall flow of your app).

声明一个属性atomic会使编译器生成额外的代码,以防止并发访问该属性。此附加代码锁定信号量,然后获取或设置属性,然后解锁信号量。与设置或获取原始值或指针相比,锁定和解锁信号量是昂贵的(尽管如果考虑应用程序的整体流程,通常可以忽略不计)。

Since most of your classes under iOS, especially the ones related to UI, will be used in a single-threaded environment, it is safe to drop atomic(i.e. write nonatomic, because properties are atomicby default): even though the operation is relatively inexpensive, you do not want to pay for things that you do not need.

由于你在 iOS 下的大部分类,尤其是与 UI 相关的类,都会在单线程环境中使用,所以删除是安全的atomic(即 write nonatomic,因为属性是atomic默认的):即使操作相对便宜,你不想为你不需要的东西买单。

回答by DharaParekh

see the difference between atomic and nonatomic in objective c

查看目标 c 中原子和非原子之间的区别

Atomic

原子

Atomic is the default behaviour for a property; by not explicitly setting the above property as nonatomic, it will be atomic.

Atomic 是属性的默认行为;通过不明确地将上述属性设置为非原子,它将是原子的。

An atomic property adds a level of thread safety when getting or setting values. That is, the getter and setter for the property will always be fully completed regardless of what other threads are doing. The trade-off is that these properties will be a little slower to access than a nonatomic equivalent.

在获取或设置值时,原子属性增加了线程安全级别。也就是说,无论其他线程在做什么,属性的 getter 和 setter 将始终完全完成。权衡是这些属性的访问速度会比非原子等价物慢一点。

Non-Atomic

非原子

Nonatomic properties are not thread safe, and will return their properties directly. This will be faster than atomic properties, but obviously carries some risk if precautions aren't made.

非原子属性不是线程安全的,会直接返回它们的属性。这将比原子属性更快,但如果不采取预防措施,显然会带来一些风险。

回答by Suryakant Sharma

@property (strong) NSString *str;

Atomicis the default behaviour for a property; by not explicitly setting the above property as nonatomic, it will be atomic.

Atomic是属性的默认行为;通过不明确地将上述属性设置为非原子,它将是原子的。

setter & getter for these Atomicproperty

这些Atomic属性的setter 和 getter

-(NSString *) str{
@synchronized(self){
return str;
}}

-(void) setStr: (NSString *) newString {
@synchronized(self)??{
str = newString;
}}

An atomic property adds a level of thread safety when getting or setting values. That is, the getter and setter for the property will always be fully completed regardless of what other threads are doing. these properties will be a little slower to access than a nonatomic equivalent.

在获取或设置值时,原子属性增加了线程安全级别。也就是说,无论其他线程在做什么,属性的 getter 和 setter 将始终完全完成。这些属性的访问速度会比非原子等价物慢一点。

@property (strong,nonatomic) NSString *str;

Nonatomicproperties are not thread safe, and will return their properties directly. This will be faster than atomic properties, but obviously carries some risk if precautions aren't made.

非原子属性不是线程安全的,会直接返回它们的属性。这将比原子属性更快,但如果不采取预防措施,显然会带来一些风险。

setter & getter for these Nonatomicproperty

这些非原子属性的setter 和 getter

-(NSString *) str{
    return str;
    }}

-(void) setStr: (NSString *) newString{
str = newString;
}

So by looking on the setter & getter methods for both Atomic & nonatomicthat nonatomic methods are very light weight.

因此,通过查看原子和非原子的 setter 和 getter 方法,原子方法的重量非常轻。