gpt4 book ai didi

objective-c - cocoa 非原子特性

转载 作者:行者123 更新时间:2023-12-03 16:53:19 25 4
gpt4 key购买 nike

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

最佳答案

非原子访问器速度更快,因为它们不必锁定。这就是全部内容了。来自 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.

关于objective-c - cocoa 非原子特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2960322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com