Different usage of shared_ptr/weak_ptr

G

Goran

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? If no, why not?

Thanks,

Goran.
 
M

mlimber

Goran said:
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? If no, why not?

Thanks,

Goran.

If you don't get a response, you might want to post this on the Boost
mailing list or some such place.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top