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-04-16 10:15:00 发布
本文详细阐述了Singleton模式的实现细节,包括线程安全、内存释放和垃圾回收策略,并通过实例展示了如何在多线程环境下正确使用Singleton模式。

1104

被折叠的 条评论
为什么被折叠?



