Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-02-29 17:12:07

Hash: SHA1

I came up with another idea for handling automatic connection management in
thread_safe_signals (thread-safe version of boost.signals). I've attached
some proof-of-concept code. The new code is supposed to enable going back to
more of a boost::signals::trackable idea, where you inherit from
a "trackable" base class to get automatic connection management. It still
requires the trackable object be managed by a shared_ptr, but now the owning
shared_ptr would not have to be explicitly passed to the slot for tracking.
Also, it should make it possible to track connections to "this" when making
connections in a constructor. Instead, the signal (or slot rather) would
create a "tracker" object for each trackable object detected by visit_each.
The tracker::lock_target() function returns a bool and a shared_ptr which can
be in one of three states:

bool is false: the trackable object has been destructed and its associated
connections should be disconnected.

bool is true, and shared_ptr is non-zero: the trackable object is alive and
its associated connections should operate normally. The returned shared_ptr
can be used to insure the trackable object doesn't destruct while the signal
invocation is in progress.

bool is true, and shared_ptr is zero: the trackable object is either
constructed but not yet owned by a shared_ptr, or it is in the process of
being destructed. In either case, the connection would be blocked until one
of the other two states is reached.

For those who have no idea what I'm talking about, I'll give a little
background. The trackable/visit_each scheme of boost.signals was abandoned
in thread_safe_signals since it disconnects the connections in the destructor
of the trackable class. This isn't thread-safe, since the trackable
destructor isn't called until after any derived class' destructors. The
replacement idea was to allow shared_ptrs to be passed to slots for tracking.
The shared_ptrs are assumed to own (directly or indirectly) any objects whose
destruction should cause the slot to automatically disconnect. The slot
keeps weak_ptr copies, and disconnects when any of the tracked weak_ptrs
expire. The slot's weak_ptrs are all be converted to shared_ptrs whenever
the slot is run, to insure the tracked objects don't destruct in the middle
of signal invocation. One limitation is an object can't make automatically
tracked connections to itself in its constructor, since
boost::enable_shared_from_this doesn't work in constructors.

- --
Version: GnuPG v1.4.6 (GNU/Linux)


Content-Type: text/x-c++src;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;

// Copyright Frank Mori Hess 2008. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at

// For more information, see

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <utility>

namespace poc
        class trackable;

        namespace detail
                class trackable_impl
                        trackable_impl(trackable &t): _trackable(t), _alive(true)
                        std::pair<bool, boost::shared_ptr<void> > lock();

                        boost::mutex _mutex;
                        trackable &_trackable;
                        bool _alive;

        class trackable: public boost::enable_shared_from_this<trackable>
                trackable(): _impl(new detail::trackable_impl(*this))
                        boost::mutex::scoped_lock lock(_impl->_mutex);
                        _impl->_alive = false;
                friend class tracker;

                boost::shared_ptr<detail::trackable_impl> _impl;

        class tracker
                tracker(trackable &target): _target_impl(target._impl)
                std::pair<bool, boost::shared_ptr<void> > lock_target()
                        return _target_impl->lock();
                boost::shared_ptr<detail::trackable_impl> _target_impl;

        namespace detail
                std::pair<bool, boost::shared_ptr<void> > trackable_impl::lock()
                        boost::mutex::scoped_lock lock(_mutex);
                        if(_alive == false) return std::make_pair(_alive, boost::shared_ptr<void>());
                                return std::make_pair(_alive, _trackable.shared_from_this());
                        catch(const boost::bad_weak_ptr &err)
                                return std::make_pair(_alive, boost::shared_ptr<void>());

class MyClass: public poc::trackable

#include <iostream>

void dump_tracker(poc::tracker &t)
        std::cerr << "tracker alive = " << t.lock_target().first << "\n" <<
                "tracker pointer = " << t.lock_target().second.get() << std::endl;

int main()
        MyClass *myobj = new MyClass();
        poc::tracker tracker(*myobj);
        boost::shared_ptr<MyClass> pointer(myobj);
        return 0;


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