Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-02-23 13:38:17


Frank Mori Hess wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Friday 23 February 2007 12:22 pm, Peter Dimov wrote:
>>
>> I actually like the suggestion of just offering a way to add the
>> tracked pointers manually. Either
>>
>> sig.connect( boost::bind( myfunction, boost::ref(coord->x) ), coord
>> );
>>
>> or
>>
>> sig.connect( boost::bind( myfunction, boost::ref(coord->x) ).track(
>> coord );
>>
>> Both look better on a syntactic level to me. In addition, I suspect
>> that signalslib::track contains a race condition that is avoided by
>> the other alternatives (but I haven't actually looked at it so I
>> might be wrong; it's possible, if somewhat tedious, to implement it
>> correctly).
>
> Would you elaborate on the race you are thinking of? Putting track
> in the connection class instead of the slot class makes races
> possible, since the connection is established before the tracked
> objects are added. Admittedly, this is only a problem if you are
> holding the tracked objects as weak_ptrs before giving them to the
> signal.

This isn't very likely since you won't be able to use coord->x if you only
have a weak_ptr. Typically you'll lock() and connect if that succeeds. The
temporary shared_ptr will ensure that there is no race. This is easy to
enforce if track() takes a shared_ptr instead of a weak_ptr.

The singalslib::track race occurs if you have

T* get_pointer( something_like_weak_ptr<T> p )
{
    return shared_ptr<T>( p ).get();
}

There is a small window where it's possible for the weak_ptr to expire while
the caller of get_pointer is holding a T*.


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