Sign in to follow this  

Boost::[scoped/shared/weak/intrusive]_ptr Questions

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just finished reading about all the "pointer wrappers" that are provided in Boost and I had some questions: (1) I read that the libraries are "thread safe". What kind of threading guarantees are made exactly? (2) It was said that weak_ptr<>'s can used to resolve cyclic references, but I didn't see this issue clearly illustrated. Can someone illustrate this problem and how it can be resolved by use of weak_ptr<>

Share this post


Link to post
Share on other sites
Quote:

(1) I read that the libraries are "thread safe". What kind of threading guarantees are made exactly?

The same as those of built-in types (that is to say, not much). See here.

Quote:

(2) It was said that weak_ptr<>'s can used to resolve cyclic references, but I didn't see this issue clearly illustrated. Can someone illustrate this problem and how it can be resolved by use of weak_ptr<>

Imagine two objects -- for example, two objects in a parent-child relationship. Parent has a pointer to all it's children. Each child has a pointer to its parent. A given pair of parent-child links is a cyclic reference -- the parent points to the child points to the parent points to the child...

Since shared_ptr is reference counted, the parent pointing to the child will keep the refcount of the child at (at least) one until the parent is destroyed. But the parent won't be destroyed until it's refcount is 0. And since the child points to the parent, the parents refcount will be at least one.

The only way to resolve this is to manually break the cycle, by nulling one of the links. Alternatively, a weak pointer can be used (probably to establish the child's pointer to the parent) -- a weak pointer is essentially a non-reference-incrementing 'view' of the relationship.

Share this post


Link to post
Share on other sites

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this