Alexander Terekhov | 1 Apr 2005 12:42
Picon

Re: Fw: [patch] Make std::tr1::shared_ptr thread-safe.

Sorry, forgot (to repeat) important point...

On Apr 1, 2005 11:26 AM, Alexander Terekhov
<alexander.terekhov <at> 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*"):

In Peter's "stage one", ref-counted mutable objects require syncrhonizing 
deleters.

> 
> 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();
>  }
> }

void g(shared_ptr<std::something> p); // can modify *p

void f() {
 shared_ptr<std::something> p(...);
 new_thread(g, p); // "auto detach", no join
}

won't work.

regards,
alexander.


Gmane