Boost logo

Boost Users :

From: Eduardo Panisset (eduardo.panisset_at_[hidden])
Date: 2008-08-16 04:24:12


 Excuse me for insisting on my question,

But I would like to ensure that I'm understanding shared_ptr correctly.

Not without additional synchronization (e.g. via a mutex with all
threads cooperating by locking before accessing the shared_ptr). But do
you really need to do that? Can you not give each thread that needs
access a separate copy of the shared_ptr?

Exactly. I give each thread a instance of shared_ptr, but the point is: the
critical region of release member function does not avoid race conditions
because It does not protect de code right below:

        if( new_use_count == 0 )
        {
            dispose();
            weak_release();
        }

How Can I ensure that the release member function wouldn't dispose a pointee
more than one time ? (release executing concurrently from different
shared_ptr instances)
How Can I ensure that I wouldn't dispose a pointee used by another
shared_prt in scope ?
(release and add_ref_lock executing concurrently from different shared_ptr
instances).

In my opinion the release member function implementation should be something
like below:
(So I could avoid the race conditions among several shared_ptr instance
sharing a commom pointee)

    void release() // nothrow
    {
        pthread_mutex_lock( &m_ );
        if (--use_count == 0)
        {
            dispose();
            weak_release();
        }
        pthread_mutex_unlock( &m_ );
    }

Eduardo Panisset.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net