Atomic Reference Counting

后端 未结 6 472
甜味超标
甜味超标 2020-12-16 12:12

I\'m trying to understand exactly how thread-safe, atomic reference counting works, for example as with std::shared_ptr. I mean, the basic concept is simple, b

6条回答
  •  死守一世寂寞
    2020-12-16 13:05

    Thread B: atomically increfs the refcount to 1.

    Impossible. To increment the reference count to one, the reference count would have to be zero. But if the reference count is zero, how is thread B accessing the object at all?

    Either thread B has a reference to the object or it doesn't. If it does, then the reference count cannot be zero. If it does not, then why is it messing with an object managed by smart pointers when it has no reference to that object?

提交回复
热议问题