在服务程序开发时,常常会用到单例模式,很多模块的实现只有一个类实例,为其他模快提供调用接口和数据,使用起来方便高效。是我在游戏服务器端开发中用的比较多设计模式之一。
由于C++的构造函数不是线程安全的。在多线程的情况下,为了防止多个线程同时创建对象,造成内存泄漏,需要我们注意进行处理接下来介绍如何避免内存泄漏。
单例模式分为懒汉模式和饿汉模式。
我们可以通过饿汉模式来避免内存的泄漏,在单例类定义时就进行类的实例化,就不会存在多个线程同时创建对象的情况。这种情况就是用空间换时间,不用在我们使用时再去实例化对象,提前消耗了资源和空间。
下面是通过模板方式实现的单例模式,如果需要用直接继承使用。
饿汉模式:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
template <class T>
class Singleton
{
public:
static T* instance() {
return m_instance;
}
static void delInstance() {
if (m_instance)
{
delete m_instance;
m_instance = NULL;
}
}
static T& getMe() {
return *instance();
}
protected:
Singleton() {};
virtual ~Singleton() {};
private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
private:
static T* m_instance;
};
template <class T>
T* Singleton<T>::m_instance = new T();
class TestA : public Singleton<TestA>
{
public:
TestA() {
num = 1;
cout << "Create Singleton TestA" << endl;
};
~TestA() {
cout << "Delete Singleton TestA" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
class TestB : public Singleton<TestB>
{
public:
TestB() {
num = 1;
cout << "Create Singleton TestB" << endl;
};
~TestB() {
cout << "Delete Singleton TestB" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
int main()
{
cout << "Start process" << endl;
int hhh = TestA::getMe().get();
TestA::getMe().add();
int bbb = TestA::getMe().get();
TestB::getMe().add();
return 0;
}
编译执行
如果我们需要用单例的时候,继承一下可以可以用了还有就是如果需要使用时再去实例化对象,就需要使用懒汉模式来实现,我们通过加锁来避免多个线程的竞争。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
template <class T>
class Singleton
{
public:
static T* instance() {
if (!m_instance) {
pthread_mutex_lock(&mutex);
if (!m_instance)
m_instance = new T();
pthread_mutex_unlock(&mutex);
}
return m_instance;
}
static void delInstance() {
if (m_instance)
{
delete m_instance;
m_instance = NULL;
}
}
static T& getMe() {
return *instance();
}
protected:
Singleton() {};
virtual ~Singleton() {};
private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
private:
static pthread_mutex_t mutex;
static T* m_instance;
};
template<typename T>
pthread_mutex_t Singleton<T>::mutex = PTHREAD_MUTEX_INITIALIZER;
template <class T>
T* Singleton<T>::m_instance = NULL;
class TestA : public Singleton<TestA>
{
public:
TestA() {
num = 1;
cout << "Create Singleton TestA" << endl;
};
~TestA() {
cout << "Delete Singleton TestA" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
class TestB : public Singleton<TestB>
{
public:
TestB() {
num = 1;
cout << "Create Singleton TestB" << endl;
};
~TestB() {
cout << "Delete Singleton TestB" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
int main()
{
cout << "Start process" << endl;
int hhh = TestA::getMe().get();
TestA::getMe().add();
int bbb = TestA::getMe().get();
TestB::getMe().add();
return 0;
}
执行结果:
对于饿汉模式,还有一种方式防止多线程构造内存泄漏,就是用pthread_once函数来实现。在muduo库中的单例实现就用pthread_once来实现的。
实现实现如下
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
template <class T>
class Singleton
{
public:
static T* instance()
{
pthread_once(&m_once, newInstance);
return m_instance;
}
static void delInstance()
{
if (m_instance)
{
delete m_instance;
m_instance = NULL;
}
}
static T& getMe()
{
return *instance();
}
protected:
Singleton() {};
virtual ~Singleton() {};
static void newInstance()
{
m_instance = new T();
::atexit(delInstance);
}
private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
private:
static T* m_instance;
static pthread_once_t m_once;
};
template <class T>
pthread_once_t Singleton<T>::m_once = PTHREAD_ONCE_INIT;
template <class T>
T* Singleton<T>::m_instance = NULL;
class TestA : public Singleton<TestA>
{
public:
TestA() {
num = 1;
cout << "Create Singleton TestA" << endl;
};
~TestA() {
cout << "Delete Singleton TestA" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
class TestB : public Singleton<TestB>
{
public:
TestB() {
num = 1;
cout << "Create Singleton TestB" << endl;
};
~TestB() {
cout << "Delete Singleton TestB" << endl;
};
void add() { num++; }
int get()const { return num; }
private:
int num;
};
int main()
{
cout << "Start process" << endl;
int hhh = TestA::getMe().get();
TestA::getMe().add();
int bbb = TestA::getMe().get();
TestB::getMe().add();
return 0;
}
执行结果如下: