On Wed, May 4, 2011 at 12:17 AM, Cory Nelson <phrosty@gmail.com> wrote:
On Tue, May 3, 2011 at 12:59 PM, Ovanes Markarian
> On Tue, May 3, 2011 at 2:18 PM, Peter Dimov <pdimov@pdimov.com> wrote:

>
> 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
>

He means that while the reference count is thread safe, the pointer is
not.  Here's an example that will cause problems:

Thread A: ptr1 = ptr2;
Thread B: ptr2 = ptr;

Note that ptr2 is being concurrently read and modified.

Oh, I see. But this looks for me as the wrong usage of shared_ptr. ptr2 should not be passed to Thread A by reference, but should be copied into the Thead A's context. Otherwise I can't imagine how this can happen. The same can happen in the single threaded environment if one uses references to shared_ptr instances.

Best Regards,
Ovanes