|
Boost Users : |
From: Joseph Thomson (joseph.thomson_at_[hidden])
Date: 2008-04-07 07:17:51
Richard L. Aurbach wrote:
>
> (1) Your idea of storing both a weak_ptr and a true pointer to the
> object strikes me as dangerous. Any time you use the true pointer
> instead of locking the weak_ptr, you run the risk that the true
> pointer is not valid. This would, of course, invalidate one of the
> major points in using the shared_ptr/weak_ptr mechanism in the first
> place. I don't think that the overhead of converting the weak_ptr via
> lock() is sufficiently high to forego the advantages of the mechanism.
> But of course, only performance testing can verify that. [My own bias
> is to build a safe mechanism first, then only weaken it if performance
> measurements verify that this step is necessary.]
>
> (2) If you have, in fact, built a mechanism where you *know* that it
> is safe to use the true pointer instead of converting the weak_ptr,
> then I wonder if starting with a shared_ptr strategy is the best way
> to go in the first place. Since you only want one lock on the file
> (rather than shared ownership), wouldn't it be better to use auto_ptr?
> That pattern seems better suited to your overall approach -- if (and
> only if) you can be sure that a user will never use a deleted true
> pointer.
>
Thanks for your reply. Well, now that I think about it my method is not
thread safe. The pointer could be deleted at any point during its use. I
may re-consider since I haven't ruled out the use of multi-threading in the
future. However, with a single thread, doesn't checking that the weak_ptr
hasn't expired before any use of the raw pointer assure that the pointer is
valid (assuming that the Object doesn't unexpectedly trigger its deletion in
one of its methods)? This method still requires use of a weak_ptr for
validation, and therefore auto_ptr wouldn't be sufficient, unless I am
missing something?
After a quick test of lock() vs. expired() vs. no check at all in my
example, with 1,000,000,000 iterations of each I get 49 seconds with no
check, 59 seconds with an expired() check, and 296 seconds when locking. So
although locking is 25 times slower, it seems sufficiently fast enough to
use in my application.
Cheers,
Joe
-- View this message in context: http://www.nabble.com/-smart_ptr--Sole-ownership-of-shared_ptr--tp16528658p16537247.html Sent from the Boost - Users mailing list archive at Nabble.com.
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