class Sigleton
{
public:
static Sigleton *GetInstance()
{
if (NULL == pInstance)
{
pthread_mutex_lock(&pMutex);
if (NULL == pInstance)
{
pInstance = new Sigleton;
}
pthread_mutex_unlock(&pMutex);
}
return pInstance;
}
static void ReleaseInstance()
{
if (NULL != pInstance)
{
pthread_mutex_lock(&pMutex);
if (NULL != pInstance)
{
delete pInstance;
pInstance = NULL;
}
pthread_mutex_unlock(&pMutex);
}
}
private:
Sigleton();
Sigleton(const Sigleton&);
Sigleton& operator=(const Sigleton&);
static Sigleton *pInstance;
static pthread_mutex_t pMutex; //= PTHREAD_MUTEX_INITIALIZER;
class CGarbo
{
public:
CGarbo() {}
~CGarbo()
{
if (NULL != pInstance)
{
pthread_mutex_lock(&pMutex);
if (NULL != pInstance)
{
delete pInstance;
pInstance = NULL;
}
pthread_mutex_unlock(&pMutex);
}
if (NULL != pMutex)
{
pthread_mutex_destory(&pMutex);
}
}
};
static CGarbo mGarbo;
};
线程安全 单例模式
最新推荐文章于 2024-07-03 18:07:54 发布