Re: naked pointer vs boost::shared_ptr<T>
On Mar 9, 3:45 pm, "James Kanze" <james.ka...@gmail.com> wrote:
In particular, weak_ptr doesn't imply shared ownership.
But it does imply that I can create a shared_ptr, and thus
obtain shared ownership. A lie, in sum.
There is a concept of temporary (usually scoped) shared ownership that
is often necessary, even when the permanent ownership is exclusive. It
comes handy in situations where the owner can destroy the object
concurrently with the clients being in the middle of operating on it.
This typically happens when multiple threads are involved, but it's
possible to encounter in single threaded, event driven (and
potentially reentrant) code as well. This is why weak_ptr only gives
you a shared_ptr, to prevent the owner from destroying the object just
after you've obtained a reference to it.
shared_ptr+weak_ptr is not the only concurrency-resilient solution to
the owner/observer problem, of course. Everyone is welcome to come up
with their own schemes. Watch out for deadlock, though.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]