工作中用到的单例类经常是用于管理,保证在内存中唯一。
1.第一种创建单例类的方式是程序加载时就创建,好处是实现简单,但程序在运行过程中始终存在不会销毁,代码实现如下:
#ifndef __SINGLETON__H_
#define __SINGLETON__H_
template <class T>
class singleton
{
public:
static T* Instance()
{
static T s_instance;
return &s_instance;
}
};
#endif
common.h定义如下
#ifndef __COMMON__H_
#define __COMMON__H_
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include "LocalDefine.h"
#include <string>
#include <iostream>
#include <stdint.h>
#include <map>
#include <vector>
using namespace std;
#if defined(WIN32) || defined(_WIN32)
#include <io.h>
#include <Ws2tcpip.h>
#include <winsock2.h>
#include <windows.h>
#include <process.h>
#include <sys/timeb.h>
#elif __linux__
#include<stdarg.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#endif
#endif
第二种是程序运行过程中动态创建,因为程序可能是多线程模式下运行,所以单例需要加锁,保证唯一。如下实现简单的锁
#ifndef __CMUTEX__H_
#define __CMUTEX__H_
#include "common.h"
class CMutex
{
public:
CMutex();
~CMutex();
void Lock();
void UnLock();
public:
#if defined(WIN32) || defined(_WIN32)
#elif __linux__
pthread_mutex_t* GetMutex() {
return &mmutex;
}
#endif
private:
#if defined(WIN32) || defined(_WIN32)
CRITICAL_SECTION mCs;
#elif __linux__
pthread_mutex_t mmutex;
#endif
};
#endif
#include "CMutex.h"
CMutex::CMutex()
{
#if defined(WIN32) || defined(_WIN32)
InitializeCriticalSection(&mCs);
#elif __linux__
pthread_mutex_init(&mmutex, nullptr);
#endif
}
CMutex::~CMutex()
{
#if defined(WIN32) || defined(_WIN32)
DeleteCriticalSection(&mCs);
#elif __linux__
pthread_mutex_destroy(&mmutex);
#endif
}
void CMutex::Lock()
{
#if defined(WIN32) || defined(_WIN32)
EnterCriticalSection(&mCs);
#elif __linux__
pthread_mutex_lock(&mmutex);
#endif
}
void CMutex::UnLock()
{
#if defined(WIN32) || defined(_WIN32)
LeaveCriticalSection(&mCs);
#elif __linux__
pthread_mutex_unlock(&mmutex);
#endif
}
而且创建单例类时需要禁用构造函数以及拷贝构造函数和赋值函数,所以单例实现如下,在子类继承中需要使用宏SINGLE_CLASS_INITIAL
#ifndef __SINGLETON__H_
#define __SINGLETON__H_
#include "CMutex.h"
#define SINGLE_CLASS_INITIAL(TypeName) private:\
TypeName();\
TypeName(const TypeName&);\
TypeName& operator=(const TypeName&);\
friend class CSingleton<TypeName>
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&)
template <typename T>
class CSingleton
{
public:
static T* Instance();
virtual ~CSingleton(){};
protected:
CSingleton(){};
private:
class CGarbo //它的唯一工作就是在析构函数中删除CSingleton的实例
{
public:
~CGarbo()
{
if (nullptr != CSingleton::mpInstance)
{
DODELETE(CSingleton::mpInstance);
}
}
};
private:
DISALLOW_COPY_AND_ASSIGN(CSingleton);
static T* mpInstance;
static CGarbo mcGarbo; //定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数
static CMutex mcStaticMutex;
};
template <typename T>
T* CSingleton<T>::mpInstance = nullptr;
template <typename T>
CMutex CSingleton<T>::mcStaticMutex;
//template <typename T>
//CSingleton<typename>::CGarbo CSingleton<T>::mcGarbo;
template <typename T>
T* CSingleton<T>::Instance()
{
if (nullptr == mpInstance)
{
mcStaticMutex.Lock();
while (nullptr == mpInstance)
{
mpInstance = new T();
}
mcStaticMutex.UnLock();
}
return mpInstance;
}
#endif