gpt4 book ai didi

objective-c - 当我发现自己 100% 的时间都在使用非原子时,为什么 "atomic"是 Objective C 中的默认 @property 限定符?

转载 作者:IT老高 更新时间:2023-10-28 11:48:19 26 4
gpt4 key购买 nike

在我作为 iOS 开发人员的几年中,我认为我从未在属性上使用过 atomic。如果我可以看到由于线程导致的潜在竞争条件或数据完整性问题,那么在 @property 上使用 atomic 将无济于事。我使用传统的事务/工作单元线程安全技术(使用机制锁、信号量或其他)。

有没有人有(或知道)使用 atomic 的实际示例? (我希望看到一些实际/实用的代码示例)

在写了十亿次nonatomic之后,我还想知道为什么Apple决定将atomic设为默认值。

最佳答案

至于你遇到的第一个问题,可能是因为

Although “atomic” means that access to the property is thread-safe, simply making all the properties in your class atomic does not mean that your class or more generally your object graph is “thread safe”—thread safety cannot be expressed at the level of individual accessor methods.

至于为什么苹果默认将其设为原子,我认为没有什么好的理由,这只是一个糟糕的设计决定。 WWDC session 上的人一再鼓励人们尽可能使用非原子来消除性能影响。

关于objective-c - 当我发现自己 100% 的时间都在使用非原子时,为什么 "atomic"是 Objective C 中的默认 @property 限定符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168331/

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