This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Fw: [patch] Make std::tr1::shared_ptr thread-safe.
- From: Alexander Terekhov <alexander dot terekhov at gmail dot com>
- To: Peter Dimov <pdimov at mmltd dot net>
- Cc: libstdc++ at gcc dot gnu dot org, Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- Date: Fri, 1 Apr 2005 11:26:24 +0200
- Subject: Re: Fw: [patch] Make std::tr1::shared_ptr thread-safe.
- References: <OF24DE43AB.98214558-ONC1256FD5.0044D61E-C1256FD5.0044BD18@de.ibm.com> <e52efbe105033105212ee0988a@mail.gmail.com> <013201c53625$27348390$6501a8c0@pdimov2> <e52efbe1050331123229a6c57d@mail.gmail.com> <e52efbe1050331160517aab37e@mail.gmail.com>
- Reply-to: Alexander Terekhov <alexander dot terekhov at gmail dot com>
On Apr 1, 2005 2:05 AM, Alexander Terekhov <alexander.terekhov@gmail.com> wrote:
[...]
> > Hmm. Now comes Whitehead (mutex inside lock())..
> >
> > Without "real" fence in lock(), I don't see how to make it safe with
> > relaxed msync protocol for self_count_.decrement() in weak_release().
Empty lock/unlock in weak_release() will take care of
thread A:
weak_p1.add_ref_lock(); // Whitehead
weak_p1.drop_reference();
thread B:
weak_p2.drop_reference();
(when both weak_p1 and weak_p2 point to the same "expired" object)
and...
>
> Mutex dtor is another issue.
...it will also take care of that issue.
So here is Peter's stage one version ("no more broken than the rest of
the library"... "will work *provided that __exchange_and_add imposes at
least the ordering that is required for reference-counted immutable
objects to work*"):
void add_ref_lock()
{
pthread_mutex_lock( &_M_mutex );
// ... Whitehead ...
pthread_mutex_unlock( &_M_mutex );
}
void release() // nothrow
{
if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
{
dispose();
pthread_mutex_lock( &_M_mutex );
pthread_mutex_unlock( &_M_mutex );
weak_release();
}
}
void weak_release() // nothrow
{
if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
{
pthread_mutex_lock( &_M_mutex );
pthread_mutex_unlock( &_M_mutex );
destroy();
}
}
regards,
alexander.