Boost logo

Boost Users :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2005-01-21 15:05:45


> > It is possible. In a multithreaded program, another thread may reset()
> > a. In a single-threaded program, a function called by test() may reset
> > a. I have encountered it myself.
>
> But doesn't that violate the thread-safety guarantee of the shared_ptr
> interface anyway? Using two different shared pointers to the same
> object is guaranteed to be thread safe, but you aren't supposed to let a
> thread modify a particular shared pointer instance while another thread
> is accessing it.
>
> The fact that it is passed by const reference in a function call is a
> red herring, since the multiple access violation can occur before or
> after the call too.

I think multithreading is red herring. It just a problem with passing by
reference (and multithreading just adds to the possibility of this to
happend):

shared_ptr<A> a;

void load() { a = new A; }
void unload() { a.reset(); }

void foo() { unload(); }

void process( shared_ptr<A> const& a )
{
    .... // here a is valid
    foo(); // this function may as well be called from other thread
    .... // here a is invalid
}

int main()
{
   load();
   process( a );
   unload();
}

Gennadiy


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