Hacker News new | past | comments | ask | show | jobs | submit login

How would it work exactly? The layout of a generic shared ptr (but not specifically std::shared_ptr) is:

   ptr -> (count, payload)
instead of:

   (count, ptr) -> payload
As you can see, count is not alongside the pointer itself as distinct instances of ptrs need to share the count.

Let's say you want to acquire an additional reference to ptr. You need to both copy the current value of ptr and increment count atomically to protect against concurrent modifications of ptr that might drop the last reference to payload.

Delaying the deallocation of payload via hazard pointers, RCU or some other deferred reclamation scheme works, but it is significantly more complex. I believe this is what the rust arc-swap package does internally.




Ah you are right, I was only considering the limited case of replacing the value when the reference count is 1.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: