Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-06-28 11:05:43


From: "Terence Wilson" <tez_at_[hidden]>
> I'm having some trouble with a container of shared_ptr's shared between
> threads. I'm seeing a crash during a pointer access. I have code
> something like this:
>
> thread 1
>
> shared_ptr<CThing> pThing(new CThing);
> .
> .
> pThing->DoSomething();
> .
> .
> pThing=shared_ptr<CThing>(new CThing)
>
>
> thread 2
>
> pThing=shared_ptr<CThing>(new CThing)
> .
> .
> pThing->DoSomethingElse();
>
> I'm using boost 1_28, Visual Studio 7 and just include the header as
> follows:
>
> #include <boost/smart_ptr.hpp>
>
> I don't build the library or declare any symbols.
>
> Is there there something special I need to do to make shared_ptr thread
> safe? Or am I barking up the wrong tree in terms of my thread safety
> expectations?

Wrong tree. If you replace 'shared_ptr<CThing>' with 'CThing*' in the above,
you'll see why it doesn't work. You must synchronize access to pThing (and
*pThing, for that matter.) shared_ptr isn't more (or less) thread safe than
an "ordinary" pointer.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk