Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr and weak_ptr concurrency
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2009-09-01 22:51:21


On Mon, Aug 31, 2009 at 8:32 PM, Rajeev Rao <rbsrao79_at_[hidden]> wrote:

> This is the first time I'm using boost in a multi-threaded env (linux
> x86_64,gcc 4).
>
> This question may have been asked in other forms. However, from what I've
> been able to search (google) up, I could not get a crystal clear answer.
> I'm essentially trying to use the solution provided in this page.
>
> http://onlamp.com/pub/a/onlamp/2006/05/04/smart-pointers.html?page=5.
>
> From the boost documentation, it appears that this could be an undefined
> operation as the global shared pointer is being read and written to by
> multiple threads. Is that correct ?
>
> In case the link fails or someone wants more details, please read on.
>
> I've got a n reader threads and 1 writer thread.
>
> initializion Thread :
> shared_ptr< MyClass > global_ptr(createNewObject()) ;
>
> WriterThread :
> global_ptr.reset( createNewObject())
>
>
> ReaderThreads:
> weak_ptr<MyClass> local_weak_ptr (globally_ptr) ;
> ...
>
> if(shared_ptr< MyClass > local_shared_ptr = local_weak_ptr.lock() ) {
> // using local_shared_ptr.
> } else {
> // recreate weak ptr from global ?
> }
>
>
>
> thanks.
>
> Rajeev
>
>
I don't think that code is thread-safe. You can modify the shared count
safely between threads, but you can't modify the pointer itself.
Tony



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