可可非原子特性

发布于 2024-09-03 18:28:00 字数 85 浏览 2 评论 0 原文

当您查看一些 Objective-C 代码时,您经常会看到定义为非原子的类属性。为什么?当您不使用线程时,它是否会给您带来一些性能提升,或者还有其他原因吗?

When you look at some Objective-C code, you often see class properties defined as non-atomic. Why? Does it give you some performance boost when you're not working with threads, or is there some other reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-09-10 18:28:00

非原子 访问器速度更快,因为它们不必锁定。这就是全部内容了。来自文档

如果您不指定nonatomic,则在引用计数环境中,对象属性的合成 get 访问器将使用锁并保留并自动释放返回值 -实现将类似于以下内容:

[_内部锁]; // 使用对象级锁进行锁定
id 结果 = [[值保留] 自动释放];
[_内部解锁];
返回结果;

如果您指定nonatomic,则对象属性的合成访问器将直接返回值。

nonatomic accessors are faster because they don't have to lock. That's about all there is to it. From the documentation:

If you do not specify nonatomic, then in a reference counted environment a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

If you specify nonatomic, then a synthesized accessor for an object property simply returns the value directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文