
Hi Boosters Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me. So basically, is there a "global" reset method? Thanks ------------------------------------------ Matthieu Guillo ------------------------------------------

Hi Boosters
Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me.
So basically, is there a "global" reset method? class A {
Matthieu Guillo 写道: public: explicit A(int v) : _v(v) { } int getV() { return _v; } private: int _v; }; shared_ptr< scoped_ptr<A> > sp_a( new scoped_ptr<A> ); (*sp_a).reset( new A(10) ); shared_ptr< scoped_ptr<A> > sp_b( sp_a ); shared_ptr< scoped_ptr<A> > sp_c( sp_a ); sp_a->reset( new A(20) ); assert( (*sp_b)->getV() == 20 ); assert( (*sp_c)->getV() == 20 );

Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me.
You can wrap ObjectA by a proxy object, so that all the shared_ptr's point to that proxy that holds actual object. Then you can "reset" the object in the proxy.

Igor R wrote:
Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me.
You can wrap ObjectA by a proxy object, so that all the shared_ptr's point to that proxy that holds actual object. Then you can "reset" the object in the proxy.
Wouldn't shared_ptr itself serve as the proxy: shared_ptr< shared_ptr<A> > aPtrPtr; Jeff

I am going to solve the same problem by replacing shared_ptr with a linked-list pointer. Each instance has pointers to two other so that all the instances pointing to the same object are linked in double linked loop. Size of such pointer is 3 times size of pointer (shared_ptr is only 2x pointer). This hardly can be done thread-safe without significant performance deradation. But it is easy to implement reset_all which changes all pointers that share the same ownership. Hi Boosters Let's say that I have many shared_ptr<A> sharing ownership to the same A object. At some point, I would like all those shared_ptr to point to a new Object A. Is there an easy (and safe) way to accomplish that? I know how to do that for one shared_ptr using either "reset", either operator = but in order to do this for all shared_ptr sharing ownership, the only way I could think of is through a registration mechanism (like the Observer pattern) but this sounds an overkilling process to me. So basically, is there a "global" reset method?

2009/2/25 Alexander
I am going to solve the same problem by replacing shared_ptr with a linked-list pointer. Each instance has pointers to two other so that all the instances pointing to the same object are linked in double linked loop. Size of such pointer is 3 times size of pointer (shared_ptr is only 2x pointer). This hardly can be done thread-safe without significant performance deradation. But it is easy to implement reset_all which changes all pointers that share the same ownership.
Common technique to reduce size of linked-list pointer is to use single linked list instead of double linked list. Copying such pointer is liner to the number of copies, but in cases where number of copies is low (i.e. in most cases) it's acceptable. The biggest advantage of linked-list pointers over shared_ptr is that they don't require additional allocation for a counter. Roman Perepelitsa.

"Roman Perepelitsa"

If you create shared_ptr with boost::make_shared, the counter and deleter would be allocated in the same block. Linked-list pointer has no significant advantages and for multi-threaded performance it is worse than shared_ptr.
True. If make_shared is used to create shared pointers, shared_ptr is better than linked-list pointer for all usages.
It has an adnvantage for this specific task: it is possible to access all the pointers that share the ownership and reset them all. Are there any other advantages of single-linked except space for a pointer ?
None that I know of. Roman Perepelitsa.
participants (6)
-
Alexander
-
Igor R
-
Jeff Flinn
-
Matthieu Guillo
-
Roman Perepelitsa
-
Wang Yun