Boost logo

Boost :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2008-03-09 05:21:10


Frank Mori Hess wrote:
>
> To have the connection disconnect on the Foo object's destruction, you're
> supposed to pass a shared_ptr owning the object (either directly or
> indirectly) to slot::track() before connecting the slot. This insures the
> object is not destroyed while a slot invocation is in progress (the signal
> converts its weak_ptr copy to a shared_ptr while the slot runs), and
> disconnects the slot when the tracked weak_ptr expires. It does have the
> drawback that you often can't track connections made in the constructor
> though, since enable_shared_from_this doesn't work there. I did provide
> postconstructible/deconstruct_ptr to support postconstructors, although it
> does all add up to a bit more typing.
>

Thanks for the clarification. This solution forces the use of shared_ptrs
and might keep a Foo instance alive a little bit longer. Especially the
latter requirement is a no-no for me.

I created my own thread safe signals implementation which requires a
"CallbackRequester" to connect a synchronous slot to an asynchronous signal:

class CallbackRequester { virtual void callLater(const
boost::function<void()>& callback) = 0; };

The callbackrequester implementation -typically a queue - switches to the
thread which the synchronous slot "belongs" to. This is of course very
intrusive to the entire design of an application - callbackrequesters
implementations need to exist for all threads and be passed around all over
the application. However, it made it possible to implement a safe
scoped_connection with RAII semantics. User code look something like this:

AsyncSignal<void()> sig;

class Foo{
Foo(boost::weak_ptr<CallbackRequester> req)
: con(sig, boost::bind(&Foo::SomeFunc, this), req) {}
 
void SomeFunc() { ...}

AsyncSignalConnection con;
};
 

Johan

-- 
View this message in context: http://www.nabble.com/-signals-threadsafe-version--Atomic-disconnects-tp15412841p15925318.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk