Ensures that a class has a single instance and provides a hook to that instance (global point of access). Here is a naive implementation.
class Singleton { public: static Singleton * instance() { if ( instance_ == NULL ) { Guard< Thread_Mutex > guard< lock_ >; // scope locking if ( instance_ == NULL ) // double-checked { instance_ = new Singleton(); } } return instance_; } private: static Singleton * instance_; // NULL initialized by the compiler static Thread_Mutex lock_; };
Singleton has a few subtle issues, because it involves different features,
A deep discussion of the issues of the Singleton can be found in the book "Modern C++ Design" by A. Alexandrescu.