
----- Original Message ----- From: "Jason Hise" <chaos@ezequal.com> To: <boost@lists.boost.org> Sent: Wednesday, January 12, 2005 11:07 AM Subject: Re: [boost] Singleton [snip]
My approach was:
1. Have a single SingletonRegistry that would keep pointers to all Singleton instances (well, actually, SingletonBase instances. SingletonBase is not a class-template.) 2. Each instance has a longevity-int associated with it. At destruction-time, we sort the singleton-instances based on their relative longevities, and destroy the objects in order. It's kinda up to the "client" of Singleton<T> to figure out the collaboration between her classes, and assign longevity-values to her classes. 3. Singleton<T> instances are "registered" with the SingletonRegistry, by its constructor. 4. Singleton<T>::~Singleton() lets the registry know that the instance may be destroyed. The SingletonRegistry destroys the instance if all singletons with a lesser longevity have already been marked for destruction. (I guess that's simple mark-and-sweep.)
I'd be honoured and grateful if anyone has comments on this approach.
At your command :-) Apologies for the delayed response but I was waiting for this to develop. Instead it seems to have gone elsewhere. After struggling with the issue of ctor+dtor of C++ globals for some time, I wonder at the efficacy of your own intentions, WRT correct dtor of singletons. While I can see where it is headed and can imagine that it will solve a set of problems, does it solve the typical problem? Assigning some ordering "longevity-int" is creating another "model of destruction". This is different to the CRT model and more significantly, configurable. The perfect ordering of global ctor+dtor is runtime dependent, i.e. the sequence of ctor's and dtor's alters from one execution to another. Here's one instance of related research by others; http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.13 If your model is different to the "real" model (ctor+dtor sequence is runtime dependent), what will you achieve with it? If someone wth a dtor dependency issue discovers your "longevity-int" feature, applies it to a complex codebase only to discover that the true problem is outside the domain of an "ordering ints" strategy, is the wasted effort entirely their fault? A ridiculous question posed to make a point. Only hoping to reduce your workload. Developing the singleton template is starting to look like a real job. Cheers, Scott