473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Different usage of shared_ptr/weak_ptr

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.

Oct 24 '05 #1
1 3330
Goran 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? 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

Oct 24 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
8
by: ralpe | last post by:
Hi, I have a question regarding boost::shared_ptr. I want to let shared_ptr take care of my objects' lifetimes, so I change all my function signatures so that they accept shared_ptrs instead of...
1
by: Raf256 | last post by:
Hi, how to convert following C++ code: foobar *ptr = new foobar(5); //... if (something()) ptr = NULL; if (!ptr) return no_object_is_selected_apparently; // ... if (!ptr) throw...
15
by: Dilip | last post by:
I am aware that the C++ standard in its present form does not say anything about threads, however I do have a relevant question. I am working on Windows XP/VC++ 8.0. Is there a problem new'ing...
3
by: Emmanuel Deloget | last post by:
Hello y'all, I'm currently writing a serie of blog tickets about the TR1, both from a definition point of view and from an implementation point of view. The next iteration in the series deals...
4
by: Juha Nieminen | last post by:
I was wondering: How will the shared_ptr type required by the upcoming C++ standard be typically implemented? An "easy" implementation is to have shared_ptr have two pointers inside it, one to...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
0
by: raj s | last post by:
below lines are from boost"Because shared pointer implementation uses reference counting, cycles of shared_ptr instances will not be reclaimed. For example, if main() holds a shared_ptr to A, which...
1
by: puzzlecracker | last post by:
When would you use which?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.