Boost logo

Boost :

From: Patrick Twohig (p-twohig_at_[hidden])
Date: 2008-04-11 17:50:10

I've been maintaining/developing a fairly large application which has been
built entirely without the use of exceptions. Two of the major target
platforms have weak or no exception support. Anyhow, It appears that the
function weak_ptr<T>::lock() relies on an exception thrown by
shared_ptr<T>'s constructor in order to work properly. I'm assuming based
on the code for lock() that the reason the call uses an exception is because
the constructor of shared_ptr uses an atomic operation to perform the
reference counting. I was curious if perhaps and appropriate solution for
compilers lacking support for exceptions would be to simply make the
constructors for boost::shared_ptr construct a shared_ptr equivalent to
shared_ptr<T> ptr( 0 ); I'm assuming that if the constructors of shared_ptr
never throw (even if the interface indicates so) using weak_ptr::lock()
would be safe based on the code below, taken directly from weak_ptr.hpp.

    shared_ptr<T> lock() const // never throws
#if defined(BOOST_HAS_THREADS)

        // optimization: avoid throw overhead
            return shared_ptr<element_type>();

            return shared_ptr<element_type>(*this);
        catch(bad_weak_ptr const &)
            // Q: how can we get here?
            // A: another thread may have invalidated r after the use_count
test above.
            return shared_ptr<element_type>();


        // optimization: avoid try/catch overhead when single threaded
        return expired()? shared_ptr<element_type>():


Boost list run by bdawes at, gregod at, cpdaniel at, john at