Which is threadsafe atomic or non atomic?

前端 未结 7 1772
广开言路
广开言路 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条回答
  •  -上瘾入骨i
    2020-12-02 07:51

    atomic guarantees atomic access to the variable but it DOESN'T make your code thread safe. Neither does non-atomic.

    With "atomic", the synthesized setter/getter methods will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. So if thread A is in the middle of the getter while thread B calls the setter, an actual viable value will be returned to the caller in A. For nonatomic, you have no such guarantees.

提交回复
热议问题