c++ - On the implementation of atomic_exchange_strong_explicit for shared_ptr -
please see this link:
template<typename _tp> bool atomic_compare_exchange_strong_explicit(shared_ptr<_tp>* __p, shared_ptr<_tp>* __v, shared_ptr<_tp> __w, memory_order, memory_order) { shared_ptr<_tp> __x; // goes out of scope after __lock _sp_locker __lock{__p, __v}; owner_less<shared_ptr<_tp>> __less; if (*__p == *__v && !__less(*__p, *__v) && !__less(*__v, *__p)) { __x = std::move(*__p); *__p = std::move(__w); return true; } __x = std::move(*__v); *__v = *__p; return false; } to me looks *__p == *__v , !__less(*__p, *__v) && !__less(*__v, *__p) both state fact pointers *__p , *__v equal. why both used there?
thanks.
as touched on by great answer need both sure both shared_ptrs share same object , share same ownership of object ( both shared pointers use same ref counter ? ).
*__p == *__v checks first part comparing *__p.get() == *__v.get() , !__less(*__p, *__v) && !__less(*__v, *__p) makes sure both shared pointers share internal ref counter, satisfying both parts.
*__p == *__v checks equality of owned objects, not shared state of internal ref counter need here, why need construct.
Comments
Post a Comment