This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [patch] Make std::tr1::shared_ptr thread-safe.
On Wed, Mar 30, 2005 at 10:23:21AM -0800, Ulrich Drepper wrote:
> Paolo Carlini wrote:
>
> >Then, readd to the library implementations of exchange_and_add for the
> >platforms that can implement it, otherwise fall back to the stop-gap
> >solution (some configury bits needed).
>
> Using exchange_and_add is dangerous, it should not be used.
>
> Granted, it will hurt only broken programs, but only those will use an
> object with zero use anyway.
>
> The problem is that is two thread simultaneously call lock, one suddenly
> succeeds since one of the exchange_and_add comes first. The result is
> the first thread will throw an exception and destroy the object while
> the second thinks all is fine and continues its work with a broken pointer.
>
> The copmare_and_exchange I used is the correct solution.
This is what I have in my working copy (where I've resurrected
compare_and_swap) but I make no claims that it's correct:
void
add_ref_lock()
{
_Atomic_word __tmp = _M_use_count;
while (__tmp)
{
if (__gnu_cxx::__compare_and_swap(&_M_use_count, __tmp, __tmp+1))
return;
__tmp = _M_use_count;
}
__throw_bad_weak_ptr();
}