作用:保证为一个类只生成唯一的实例对象。也就是说,在整个程序空间中,该类只存在一个实例对象
实现单例模式的步骤:
(1)将构造方法私有化,使其不能在类的外部通过new关键字实例化该类对象。
(2)在该类内部产生一个唯一的实例化对象,并且将其封装为private static类型;保证程序中只有一块内存
(3)定义一个静态方法返回这个唯一对象;可以通过类名来调用
单例模式有两类:懒汉式和饿汉式
懒汉式与饿汉式的区别:懒汉式是先定义,new的时候在分配内存;饿汉式是在定义时就分配内存。懒汉式的这种特点会导致在多线程的时候,如果同时分配内存,彼此之间并不能够及时检查内存是否已经分配,导致出现多个实例对象,这显然与我们的要求不符,可以使用互斥锁等方法。
小知识:synchronized 关键字(这是java里的),代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。 ---------引用百度百科
懒汉式:(已使用互斥锁)
/*************************************************************************
> File Name: singleton.cpp
> Author: ls
> Mail: 1096475833@qq.com
> Created Time: 2019年07月07日 星期日 12时24分29秒
************************************************************************/
#include<iostream>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<stdio.h>
using namespace std;
pthread_mutex_t mutex;
class Singleton
{
private:
static Singleton*mInstance; //1、需要静态成员变量,保证程序中只有一块内存
static int count;
Singleton (){} //2、构造函数一定是私有的,保证不能在类的外部创建对象
public:
static Singleton*GetInstance() //3、创建对象的函数一定是静态的,可以通过类名调用
{
count++;
pthread_mutex_lock(&mutex);
if(mInstance == NULL)
{
usleep(10000);
mInstance = new Singleton; //在类的内部创建对象
}
pthread_mutex_unlock(&mutex);
return mInstance;
}
int GetCount()
{
return count;
}
void Release()
{
count--;
if(count == 0 && mInstance != NULL)
{
delete mInstance; //释放最后一个对象并清空内存
}
}
};
Singleton* Singleton::mInstance = NULL;
int Singleton::count = 0;
void *CreateInstance(void *arg)
{
Singleton *s = Singleton::GetInstance();
cout<<s<<endl;
}
int main()
{
/*Singleton*p = Singleton::GetInstance();
cout<<p<<endl;
Singleton*p1 = Singleton::GetInstance();
cout<<p1<<endl;
Singleton*p2 = Singleton::GetInstance();
cout<<p2<<endl;*/
int ret,i;
pthread_t tid[5];
pthread_mutex_init(&mutex,NULL);
for(i = 0;i<5;++i)
{
ret = pthread_create(&tid[i],NULL,CreateInstance,NULL);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
}
for(i = 0;i<5;++i)
{
void *status;
pthread_join(tid[i],&status);
}
pthread_mutex_destroy(&mutex);
return 0;
}
饿汉式
/*************************************************************************
> File Name: singleton.cpp
> Author: ls
> Mail: 1096475833@qq.com
> Created Time: 2019年07月07日 星期日 12时24分29秒
************************************************************************/
#include<iostream>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<stdio.h>
using namespace std;
class Singleton
{
private:
static Singleton*mInstance; //1、需要静态成员变量,保证程序中只有一块内存
static int count;
Singleton (){} //2、构造函数一定是私有的,保证不能在类的外部创建对象
public:
static Singleton*GetInstance() //3、创建对象的函数一定是静态的,可以通过类名调用
{
count++;
return mInstance;
}
int GetCount()
{
return count;
}
void Release()
{
count--;
if(count == 0 && mInstance != NULL)
{
delete mInstance; //释放最后一个对象并清空内存
}
}
};
Singleton* Singleton::mInstance = new Singleton;
int Singleton::count = 0;
int main()
{
Singleton*p = Singleton::GetInstance();
cout<<p<<endl;
Singleton*p1 = Singleton::GetInstance();
cout<<p1<<endl;
Singleton*p2 = Singleton::GetInstance();
cout<<p2<<endl;
cout<<p2->GetCount()<<endl;
p2->Release();
cout<<p1->GetCount()<<endl;
return 0;
}