[c++]代码库
#ifndef SINGLETON_H
#define SINGLETON_H
#include "synobj.h"
class Singleton {
public:
static Singleton& Instance() { // Unique point of access
Lock lock(_mutex);
if (0 == _instance) {
_instance = new Singleton();
atexit(Destroy); // Register Destroy function
}
return *_instance;
}
void DoSomething(){}
private:
static void Destroy() { // Destroy the only instance
if ( _instance != 0 ) {
delete _instance;
_instance = 0;
}
}
Singleton(){} // Prevent clients from creating a new Singleton
~Singleton(){} // Prevent clients from deleting a Singleton
Singleton(const Singleton&); // Prevent clients from copying a Singleton
Singleton& operator=(const Singleton&);
private:
static Mutex _mutex;
static Singleton *_instance; // The one and only instance
};
#endif/*SINGLETON_H*/