简单单例类

工作中用到的单例类经常是用于管理,保证在内存中唯一。

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值