Which is threadsafe atomic or non atomic?

前端 未结 7 1773
广开言路
广开言路 2020-12-02 07:05

I searched and found immutable are thread safe while mutable is not. This is fine. But i got misleading notes, blogs, answers about atomic vs non-atomic about thread safety,

7条回答
  •  情深已故
    2020-12-02 07:54

    There's another property of "atomic" that wasn't mentioned which was needed for thread safety before ARC (and probably still is). First explaining why it's needed: Let's say without ARC, you read an object property and immediately retain it. But another thread could come in just between you reading the property and the retain call, set the object property to nil, causing the object to be deallocated. You send retain to a deallocated object, which is unhealthy. And it would be a very rare bug because it only happens when the timing is just right. To prevent this, atomic object properties always return autoreleased objects.

提交回复
热议问题