Peter,

can you please give an example how that can happen, given the shared_counter is atomic. I thought the counter is first incremented than the rest of the machinery deals with pointer copying etc. and in case of underlying object destruction, the counter is first decremented and than the object is destroyed. How is that possible, that shared_counter reaches 0, destructor is called and afterwards the underlying pointer will be assigned to the new shared instance?

 

With Kind Regards,

Ovanes

---

 

The main point that comes to mind is because the example I give is specifically constructed to point out that the destruction of the object in the second thread is a refcount decrement only, and does not result in destruction of the shared object itself, only the shared_ptr instance.

 

Hence the potential race condition does not apply to my example. The actual destruction of the shared object occurs in a situation where I know there will not be a simultaneous “copy”.

 

Of course, Peter will provide his own answer.