Wednesday, August 19, 2009

c++: singelton

------------memMgr--LogMgr----

class foo
{
public:
static foo* instance();
private:
foo() {}
~foo() {}
static foo* smInstance;
static Lock smMutex;
};

foo* foo::smInstance = NULL;
Lock foo::smMutex;

foo* foo::instance()
{
LockLocker lock(&smMutex);

if (smInstance == NULL)
smInstance = new foo();

return smInstance;
}

No comments:

Post a Comment