Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Smart_ptr] global_reset (revamp) for shared_ptr
From: John Dlugosz (JDlugosz_at_[hidden])
Date: 2009-08-18 14:47:13


> PROBLEM
>
> I recently needed a 'shared_ptr' that could be globally reset -- that
> is, any one of the owners of the controlled resource could unilaterally
> replace that resource for the benefit (perhaps) of everyone else.
>

The replacement has to be able to deal with code that is in the process of using another smart pointer to the original object.

I would solve this by having uses mark their in-use scope by simply fetching a local copy.

That is, consider the explicit usage protocol:

   shared_ptr<mytype> global;

 to use:
   shared_ptr<mytype> local= global; // (A)
   local->foo();
   // let local go out of scope

 to update:
   global= newvalue; // (B)

Table a couple threading issues for the moment.
I'd think of an approach that automated the "use" by having an operator-> that returned a shared_ptr.
And, encapsulate the update step as a member function as well, so any needed locking or added steps can be accommodated.

Back to the issues: I don't recall whether the shared_ptr documentation is clear as to whether multiple threads can issue (A) at the same time. Multiple threads executing (B) is not allowed. And whether (A) with one (B) can occur at the same time is probably related to multiple (A)'s, and certainly not specified in the standard.

So, use a light-duty critical section of some kind to protect both of these.
After getting that to work (portably), I'd see about eliminating the critical section around (A) if the implementation supports multiple (A)'s, by using a "swap" for B, and protecting against multiple calls to update.

I've seen this pattern in the past. But we didn't use a special class to deal with it. Rather, the global thing was fetched with an accessor (so only a local smart pointer could be used by client code) and the update was only done internally to the library.

TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.


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