Boost logo

Boost Users :

Subject: Re: [Boost-users] Thread Safety of shared_ptr
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2011-05-03 15:59:06


On Tue, May 3, 2011 at 2:18 PM, Peter Dimov <pdimov_at_[hidden]> wrote:

> Kevin Frey wrote:
>
> 2. One thread A creating another shared_ptr to the shared object,
>> attempting to increment the reference count.
>>
>> 3. Another thread B simultaneously destroying a *different*
>> shared_ptr which is connected to the same underlying shared object,
>> decrementing the reference count.
>>
>
> This is OK. Manipulating different shared_ptr instances in different
> threads is fine. The example states that you can't, in thread B, destroy the
> shared_ptr which thread A copies (*sp_Session in your code).

[...]
>

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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net