Re: How do I make a thread-safe copy-constructor?
On Apr 27, 1:47 am, rich_sposato <r...@richsposato.com> wrote:
I came to a similar conclusion based on my own experiments. The
destructor in thread-1 can run through completion before thread-2 even
has a chance to enter the copy-constructor. The end result is that
the reference passed into the copy-constructor is already dead.
Even admitting that nothing can prevent that situation, ... I still
want to know if an implementation exists such that the copy-
constructor succeeds in all situations where thread-1 gets swapped out
while inside the destructor.
You can implement such a class by serializing all member functions
with a mutex, but it would still not solve your problem, since the
destructor will simply run to completion.
I looked at Boost's shared_ptr to see how that solves the problem.
[...]
As soon as I saw that inside Boost's implementation details, I stopped
assuming that Boost was thread-safe.
There are various definitions of thread safe, and a boost::shared_ptr
instance provides only the basic level (same as a raw pointer), as
explained in its documentation. That said, what you want does not meet
any reasonable definition of "thread safe". A race against a
destructor, even if completely serialized, leads to undefined
behavior. It's never thread safe.
You can use shared_ptr<Object> to make sure that nothing races against
~Object, though. This has nothing to do with shared_ptr's inability to
withstand destructor races since there would be none.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]