-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 01 February 2007 11:24 am, Timmo Stange wrote:
Frank Mori Hess wrote:
Thread-safe automatic disconnect from non-static member functions seems like it would require some kind of extension to the language itself, like a pre-destructor.
There is also the possibility of using a wrapper template instead of a base class to put the tracking functionality to the top of the class hierarchy, but I don't think I particularly like that approach, especially as measures to "finalize" the hierarchy would have to be taken.
One solution is to overload the signal::connect function with another version that accepts an additional weak_ptr<void> argument, which points to the object whose member function is being used as a slot. The signal could try to convert the weak_ptr into a shared_ptr<void> whenever it runs the slot. Then the signal can either clean up the connection if the object is dead, or be sure the object won't die while the slot is running. The disadvantage, of course, is it forces people to use shared_ptrs to manage the lifetimes of objects they want to use thread-safe automatic connection management with. But that seems preferable to offering no means of thread-safe automatic connection management at all.
Thanks for the code, it looks very promising. I was hoping it could be done with a little less runtime overhead (per slot mutex + polymorphism + shared_ptr), but I have to investigate the available options some more.
I imagine it could be. Optimization wasn't high on my priority list while writing it, I was aiming more for correctness, followed by ease of implementation.
I have one small suggestion for now: You could perhaps move the connection mutex to the base class to avoid too much per-Signature code repetition (use a private virtual do_disconnect() for forwarding, for example).
I'm not following what you're suggesting. Would you spell it out in more detail, or even better say it with a patch? - -- Frank -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFFwk7B5vihyNWuA4URAqa+AJ9b35Dk01ZrWKpqPxp0uzGp99DcBwCg04UB kIA0X8mCMjrMlfmkYeQ7Ask= =IB4D -----END PGP SIGNATURE-----