Boost logo

Boost Users :

Subject: Re: [Boost-users] Thread Safety of shared_ptr
From: Michael Caisse (boost_at_[hidden])
Date: 2011-05-03 00:11:35


On 05/02/2011 05:46 PM, Kevin Frey wrote:
>
> Unfortunately none of this helps solve my original question, which
> given the direction this thread is proceeding, I will re-state in
> pseudo-code that approximates the **actual** situation occurring in my
> program:
>
> Earlier on:
>
> shared_ptr< Session >* sp_Session = new shared_ptr< Session >( new
> Session );
>

Whoa! ... This isn't how shared pointers works. You want:

shared_ptr< Session > sp_Session = shared_ptr< Session >( new Session );

> Dependent_Class* dep = new Dependent_Class( shared_ptr< Session >&
> shared_ptr_session );
>
> In Dependent_Object::Dependent_Object( )
>
> this->sp_Session = new shared_ptr< Session >( shared_ptr_session );
>
> Now, simultaneously:
>

And... you don't want to be passing them around via reference. That
completely defeats the purpose.

> So, we have:
>
> 1. A shared object (pointed to by a shared_ptr) that is presently in
> existence with a reference count of at least 2.
>
> 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.
>
> Note that the shared object is **not being destroyed** here, just the
> reference counts are being manipulated.
>
> Will shared_ptr handle this situation correctly?
>

It is difficult to answer you actual concerns given the way you are
presenting the usage of shared_ptr. Take a closer look at the shared_ptr
docs and a few examples to get a handle on *how* the utility is used. A
general answer to your question is that reference counting is atomic
with shared_ptr.

See if you can re-formulate you question with proper usage so we can
better understand what you are wanting to do.

michael

-- 
In the Sacramento/Folsom area?
** Profesional C++ Training mid-May **
http://www.objectmodelingdesigns.com/boostcon_deal.html
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

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