Is an atomic property thread safe? [duplicate]

☆樱花仙子☆ 提交于 2019-12-23 06:36:49

问题


I have gone through many answers about atomic and non-atomic properties. But I'm not able to understand whether atomic properties are thread safe? Please explain it with an example.


回答1:


Yes

an / one atomic property is thread-safe. That is what atomicity stands for.

CAUTION

But neither are two atomic properties thread-safe in regards to each other nor are the contents of an atomic property thread-safe. (sounds a bit confusing but has be said)

That means that your are always guaranteed to be able to read a fully functional value from the property, no broken pointer, or intermediary null or whatsoever.

BUT you are not guaranteed that the values inside that atomic property are thread-safe whatsoever. That is a completely different topic.

Making all properties of a class atomic will not at all make the class itself thread-safe.




回答2:


The property accessors are thread-safe. Basically an atomic property is equivalent to this:

- (id)atomicProperty {
    @synchronized(self) {
        return _atomicProperty;
    }
}

- (void)setAtomicProperty:(id)atomicProperty {
    @synchronized(self) {
        _atomicProperty = atomicProperty;
    }
}


来源:https://stackoverflow.com/questions/33723255/is-an-atomic-property-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!