单例模式:一个类只有一个实例对象存在。
单例模式一般有两种实现方式,一种称为懒汉式,另一种称为饿汉式。
1. 懒汉式单例模式
懒汉式单例模式的思想很简单,在或者单例对象的时候总是去判断对象是否已经生成,如果对象已经生成,那么就直接返回对象的指针。
#include <iostream>
using namespace std;
//懒汉式 单例模式
class Singleton
{
private:
Singleton(){
cout << "构造函数" << endl;
}
public:
static Singleton* getInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singleton;
}
return m_Instance;
}
static void deleteInstance()
{
if (m_Instance != NULL){
delete m_Instance;
m_Instance = NULL;
}
}
private:
static Singleton* m_Instance;
};
Singleton* Singleton::m_Instance = NULL;
int main()
{
Singleton* p1 = Singleton::getInstance();
Singleton* p2 = Singleton::getInstance();
Singleton::deleteInstance();
if (p1 == p2)
{
cout << " == " << endl;
}
return 0;
}
上述代码只能适合单线程情况,试想,在多线程情况下,当前正好有两个线程同时去获取实例,然而又恰好在这之前又没有生成任何的对象实例。也就是说这个两个线程都会判断得出对象实例不存在,然后他们都会去创建一个实例。
这一过程就会创建出两个实例,而且先创建的实例是没有指针指向他的,也不会有销毁的操作,所以就会导致内存的泄露。
要解决这个问题就是要想办法让一个线程在判断是否有对象产生的时候让其他的线程都处于等待状态。
2.懒汉式单例模式多线程情况
比较好的方式就是使用线程“锁”的方式来对线程的访问进行限制约束。从而达到只创建一个对象的目的。
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <process.h>
#include <mutex>
using namespace std;
mutex mtx;
class Singleton
{
private:
Singleton(){
cout << "构造函数" << endl;
}
~Singleton()
{
if (m_Instance != NULL)
{
delete m_Instance;
m_Instance = NULL;
}
}
public:
static Singleton* getInstance()
{
if (m_Instance == NULL) //两次检查,解决懒汉式多线程中的创建多个对象的问题
{
mtx.lock();
if (m_Instance == NULL)
{
m_Instance = new Singleton;
++count;
}
mtx.unlock();
}
return m_Instance;
}
static void deleteInstance() //销毁单例对象
{
if (m_Instance != NULL){
delete m_Instance;
m_Instance = NULL;
}
}
void print()
{
cout << count << endl;
}
private:
static Singleton* m_Instance;
static int count;
};
Singleton* Singleton::m_Instance = NULL;
int Singleton::count = 0;
void threadfunc(void *myIpAdd)
{
int id = GetCurrentThreadId();
Singleton::getInstance()->print();
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThrdParam = 1;
HANDLE hThread[10];
int threadnum = 3;
for (int i = 0; i < threadnum; i++)
{
hThread[i] = (HANDLE)_beginthread(&threadfunc, 0, 0);
if (hThread[i] == NULL)
break;
}
for (int i = 0; i < threadnum; i++)
{
WaitForSingleObject(hThread[i], INFINITE);
}
Singleton::deleteInstance();
return 0;
}
3. 饿汉式单例模式
饿汉式不同于懒汉式的是,在程序运行一开始就去创建一个单例实体。这样在后面的使用中就都不用去管对象是否创建,也不用去管多线程下创建多个实例的情况。显然,饿汉式是一种用空间来换取时间的做法。
//懒汉式 单例模式
class Singleton
{
private:
Singleton(){
cout << "构造函数" << endl;
}
public:
static Singleton* getInstance()
{
return m_Instance;
}
static void deleteInstance()
{
if (m_Instance != NULL){
delete m_Instance;
m_Instance = NULL;
}
}
private:
static Singleton* m_Instance;
};
Singleton* Singleton::m_Instance = new Singleton;
本文介绍了单例模式的两种实现方式:懒汉式和饿汉式。详细解释了懒汉式单例模式在多线程环境下可能存在的问题及解决方案,并对比了饿汉式单例模式的特点。
209

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



