一、单例模式
什么叫单例模式
就是一个类有且只有一个对象,并且只能通过接口获取到该对象
所以设计该类时,需要满足以下条件
1.构造函数私有化
2.禁止赋值和拷贝
3.提供获取到该类的接口
二、代码演示
1、简单的单例模式实现
#include <iostream>
using namespace std;
class MyClass
{
private:
MyClass(){ }; // 私有化构造函数
MyClass(MyClass&) = delete; // 禁止拷贝构造函数
MyClass& operator=(const MyClass&) = delete; // 禁止赋值运算符
public:
~MyClass() { };
public:
static MyClass* instance() { // 单例接口函数
if (nullptr == _instance) {
_instance = new MyClass();
}
return _instance;
}
private:
static MyClass *_instance;
};
MyClass *MyClass::_instance = nullptr;
int main(int arg, char **argv)
{
MyClass* a = MyClass::instance(); // 获取实例
MyClass* b = MyClass::instance(); // 获取实例
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
*调试结果: a 和 b 是一样的,说明是同一个对象

2.如果在多线程下使用呢,我们修改下代码
#include <iostream>
#include <thread>
using namespace std;
class MyClass
{
private:
MyClass() // 私有化构造函数
{
// 假设初始化时需要的时间
this_thread::sleep_for(chrono::milliseconds(100));
};
MyClass(MyClass&) = delete; // 禁止拷贝构造函数
MyClass& operator=(const MyClass&) = delete; // 禁止赋值运算符
public:
~MyClass() { };
public:
static MyClass* instance() { // 单例接口函数
if (nullptr == _instance) {
_instance = new MyClass();
}
return _instance;
}
private:
static MyClass *_instance;
};
MyClass *MyClass::_instance = nullptr;
MyClass *a = nullptr;
MyClass *b = nullptr;
// 线程函数1
void threadFun1()
{
a = MyClass::instance(); // 获取实例
}
// 线程函数2
void threadFun2()
{
b = MyClass::instance(); // 获取实例
}
int main(int arg, char **argv)
{
// 开启2个线程,差不多同时去访问接口
thread t1(threadFun1);
thread t2(threadFun2);
t1.detach();
t2.detach();
// 测试使用延迟一会儿。
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
*调试结果:发现 a 和 b 不相等了

这与我们单例模式不符合,那这时如何去改进呢?
没错
我们给它上锁
接下来修改一下代码
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
static mutex _mutex;
class MyClass
{
private:
MyClass() // 私有化构造函数
{
// 假设初始化时需要的时间
this_thread::sleep_for(chrono::milliseconds(100));
};
MyClass(MyClass&) = delete; // 禁止拷贝构造函数
MyClass& operator=(const MyClass&) = delete; // 禁止赋值运算符
public:
~MyClass() { };
public:
static MyClass* instance() { // 单例接口函数
if (nullptr == _instance) {
_mutex.lock(); // 加锁
if (nullptr == _instance) {
_instance = new MyClass();
}
_mutex.unlock(); // 解锁
}
return _instance;
}
private:
static MyClass *_instance;
};
MyClass *MyClass::_instance = nullptr;
MyClass *a = nullptr;
MyClass *b = nullptr;
// 线程函数1
void threadFun1()
{
a = MyClass::instance(); // 获取实例
}
// 线程函数2
void threadFun2()
{
b = MyClass::instance(); // 获取实例
}
int main(int arg, char **argv)
{
// 开启2个线程,差不多同时去访问接口
thread t1(threadFun1);
thread t2(threadFun2);
t1.detach();
t2.detach();
// 测试使用延迟一会儿。
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
*调试结果:我们发现 a 和 b 相等了

但,这样就好了么?
我们在修改一下代码,在析构函数和构造函数里添加一句输出语句
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
static mutex _mutex;
class MyClass
{
private:
MyClass() // 私有化构造函数
{
// 假设初始化时需要的时间
this_thread::sleep_for(chrono::milliseconds(100));
cout << "我是构造函数" << endl;
};
MyClass(MyClass&) = delete; // 禁止拷贝构造函数
MyClass& operator=(const MyClass&) = delete; // 禁止赋值运算符
public:
~MyClass() { cout << "我是析构函数" << endl; };
public:
static MyClass* instance() { // 单例接口函数
if (nullptr == _instance) {
_mutex.lock(); // 加锁
if (nullptr == _instance) {
_instance = new MyClass();
}
_mutex.unlock(); // 解锁
}
return _instance;
}
private:
static MyClass *_instance;
};
MyClass *MyClass::_instance = nullptr;
MyClass *a = nullptr;
MyClass *b = nullptr;
// 线程函数1
void threadFun1()
{
a = MyClass::instance(); // 获取实例
}
// 线程函数2
void threadFun2()
{
b = MyClass::instance(); // 获取实例
}
int main(int arg, char **argv)
{
// 开启2个线程,差不多同时去访问接口
thread t1(threadFun1);
thread t2(threadFun2);
t1.detach();
t2.detach();
// 测试使用延迟一会儿。
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
*调试结果:

我们发现
该类的实例没有被析构
也就是需要我们手动去析构
但有时候我们忘记去析构或者处于其他原因没有被析构就会造成内存泄漏
那我们又怎么去改进呢?
使用智能指针
嗯,使用智能指针可以完成这个析构过程
但本次不使用智能指针来完成
而是使用局部静态变量来完成
我修改一下代码
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
static mutex _mutex;
class MyClass
{
private:
MyClass() // 私有化构造函数
{
// 假设初始化时需要的时间
this_thread::sleep_for(chrono::milliseconds(100));
cout << "我是构造函数" << endl;
};
MyClass(MyClass&) = delete; // 禁止拷贝构造函数
MyClass& operator=(const MyClass&) = delete; // 禁止赋值运算符
public:
~MyClass() { cout << "我是析构函数" << endl; };
public:
static MyClass& instance() { // 单例接口函数
static MyClass _instance;
return _instance;
}
};
MyClass *a = nullptr;
MyClass *b = nullptr;
// 线程函数1
void threadFun1()
{
a = &MyClass::instance(); // 获取实例
}
// 线程函数2
void threadFun2()
{
b = &MyClass::instance(); // 获取实例
}
int main(int arg, char **argv)
{
// 开启2个线程,差不多同时去访问接口
thread t1(threadFun1);
thread t2(threadFun2);
t1.detach();
t2.detach();
// 测试使用延迟一会儿。
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
*调试结果 :可以看到达到了预期的效果

三、_End
把它修改为 static 方式后,不用线程锁也不用手动去释放了
不好的地方可能就是需要用引用的方式获取实例
目前来说,本人觉得该方式应该是比较好的一种方式了
至于什么懒汉式,饿汉之类的区分。。。
这里就不区分了。。。。
理解就好了。
...
本文介绍了C++中的单例模式,强调其确保类只有一个实例并提供全局访问点的设计原则。通过逐步演示,从简单的单例实现到解决多线程问题,最后提出使用局部静态变量优化方案,避免了线程锁和手动析构的问题,但可能需要通过引用获取实例。

2352

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



