// the double check pattern
template<class TYPE, class LOCK>
TYPE* Singleton<TYPE, LOCK>::Instance() {
// this one is for performance
if (_instance == 0) {
Guard<LOCK> monitor(_lock);
// and this one is for thread safeness
if (_instance == 0)
_instance = new TYPE;
}
return _instance;
}
D. Schmidt in:
J. Vlissides, "To Kill a Singleton,"
C++ Report, vol. 8, pp. 10-19, June 1996.
A note:
the pointer assignment needs to be atomic, which is of course not
guaranteed by the C++ language.
One has to ask: how expensive is a lock?
An additional note: don't use the double check pattern, it's broken.