为什么Cocoa的IBOutlet属性默认是原子的,而Cocoa Touch的不是? [英] Why are Cocoa's IBOutlet properties atomic by default, and Cocoa Touch's aren't?

查看:134
本文介绍了为什么Cocoa的IBOutlet属性默认是原子的,而Cocoa Touch的不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将Interface Builder中的新插件拖动到接口(标题)文件,Xcode 4.6会自动为您创建一个属性...

If you drag a new outlet from Interface Builder to an interface (header) file, Xcode 4.6 will automatically create a property for you...

Cocoa Touch)看起来像这样:

On iOS (Cocoa Touch) it will look like this:

@property (weak, nonatomic) SomeClass *someProperty; //nonatomic accessors

而在OS X(Cocoa)上,它看起来像这样:

Whereas on OS X (Cocoa) it will look like this:

@property (weak) SomeClass *someProperty; //atomic accessors (implicitly)

为什么?

EDIT :我不是问什么原子是做什么或不做什么,我很清楚synchronized指令和底层互斥(或锁或任何),保证原子性setter和getter。我知道在iOS上,访问器是非原子的,因为UIKit不是线程安全的,所以没有什么可以通过使它们原子,只是浪费处理器时间和电池寿命。我在这里谈论默认情况,程序员知道他们在做什么将知道什么时候他们需要使他们的访问器原子。

EDIT: I am not asking about what atomic does or doesn't do, I'm well aware of the synchronize directive and the underlying mutex (or lock or whatever) that guarantees atomicity of the setter and getter. I know that on iOS, accessors are nonatomic because UIKit is not thread safe, and so there is nothing to be gained by making them atomic, it's just a waste of processor time and battery life. I am talking about the default case here, programmers who know what they are doing will know when they need to make their accessors atomic.

所以我问,为什么他们在OS X上默认是原子的。我的印象是,Appkit不是线程安全的。有了原子访问器不保证线程安全,我甚至去说,它的方式相反,它可以给新手程序员的线程安全的错觉,并通过延迟崩溃在并发应用程序中的错误跟踪更困难到后来的时间,这样做使他们更难跟踪。而只是因为台式电脑相对强大并不意味着资源应该浪费(注意,我不是在谈论过早的优化),因为它是理由苹果工程师是合理的程序员,必须有一个很好的理由,他们有决定使属性默认合成原子访问器。

So I'm asking why they are atomic by default on OS X. I was under the impression that Appkit was not thread safe either. And having atomic accessors doesn't guarantee thread safety, I'd even go as far as to say it goes the opposite way in that it can give the illusion of thread safety to novice programmers and make bug tracking harder in concurrent apps by deferring crashes to a later time and in so doing making them harder to trace. And just because desktop computers are relatively powerful doesn't mean resources should be wasted (note I am not talking about premature optimization here), and since it stands to reason that Apple engineers are reasonable programmers, there must be a good reason why they have decided to make the properties synthesize atomic accessors by default.

推荐答案

在这上下文中, atomic 说明符告诉编译器setter和访问器应该被合成,以便从多个线程调用是安全的。

In this context the atomic specifier tells the compiler that the setter and accessor should be synthesised so as to be safe to call from multiple threads. This adds a small overhead by requiring the methods to take out a lock before a properties value can be written or read.

由于UIKit和Cocoa的用户界面元素只有一个属性值,因此在属性值被写入或读取之前需要取出一个锁。曾经打算从主线程访问额外的锁是不必要的。制作属性原子的开销是非常少的,但在iOS的更受约束的环境中每一个盎司的速度是有价值的..因此为什么iOS默认使用非原子属性IB Outlets。

Since user interface elements of both UIKit and Cocoa are only ever intended to be accessed from the main thread the extra lock is unnecessary. The overhead of making a property atomic is pretty minimal, but in the more constrained environment of iOS every little ounce of speed is valuable.. hence why iOS defaults to using nonatomic properties for IB Outlets.

已针对您的展开问题进行了修改:我的感觉是使用原子属性的成本值得在Mac上的开销。 Theres是一个参数,使用原子属性掩盖了bug的集合,因此是一件坏事。我认为从用户的角度,苹果应该设置默认值,以便即使严重编码的应用程序崩溃更少。它让高级程序员知道如何使用非原子属性来换取性能优势。

Edited in response to your expanded question: My feeling is that the cost of using atomic properties is worth the overhead on the Mac. Theres an argument that using atomic properties masks a collection of bugs and is therefore a bad thing. I'd argue that from a users perspective Apple should set the defaults so that even badly coded applications crash less. It puts the onus on advanced programers to know when it's safe to use nonatomic properties in exchange for a performance advantage.

如果没有团队成员的听力,我们可以只是推测他们的思维过程,但我确信这是一个被认为是解散。

Without hearing from someone on the team at the time we can only speculate about their thought process but I'm sure it was a considered decission.

这篇关于为什么Cocoa的IBOutlet属性默认是原子的,而Cocoa Touch的不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆