Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-10-26 13:03:23


Goran Pusic wrote:
> Hi all!
>
> I am trying to use boost smart pointers for some multithreaded work
> in the following way:
>
> 1. I use shared_ptr to wrap stuff in thread 1
> 2. I create weak_ptr-s in thread 1 and pass them to thread 2
> 3. in thread 2, I lock my weak_ptr-s and use stuff I get from there
> 4. in thread 1 I may destroy my shared_ptr
> 5. next time I want to lock my weak_ptr, I get NULL and can't use
> stuff
>
> Basically, my idea is to use weak_ptr as sort of transport mechanism
> between threads, but with "stuff destroyed detection".
>
> I did some tryouts and this seems to be OK. However, I'd like to get
> some more authorative advice :-)) Can I do that?

Yes, should be able to do this. You have a potential visibility problem in:

> 2. I create weak_ptr-s in thread 1 and pass them to thread 2

but it's not shared_ptr or weak_ptr specific. The mechanism that you use for
passing needs to provide the necessary visibility guarantees (so that thread
2 can see the stuff that thread 1 initialized.)


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