C++单例,线程安全,资源释放

//非线程安全;
class Singleton {
public:
	static Singleton& GetInstance() {
		static Singleton instance;//对象构造过程中可能线程切换到另一个线程;
		return instance;
	}
	void test() { cout << "test!!!" << endl; }
private:
	Singleton() {}
	Singleton(const Singleton& );
	Singleton& operator=(const Singleton& );
};

//懒汉模式:非线程安全,第一次调用才进行初始化;
class SingletonLazy {
public:
	static SingletonLazy* GetInstance() {
		if (spts == nullptr)
			// 有可能在此时出现线程切换,而此时spts为空;
			spts = new SingletonLazy();
		return spts;
	}
	void test() { cout << "class lazy!!!" << endl; }
private:
	static SingletonLazy* spts;
	SingletonLazy(){}
	SingletonLazy(const SingletonLazy&);
	SingletonLazy& operator=(const SingletonLazy&);
};

SingletonLazy* SingletonLazy::spts = nullptr;

//线程安全的懒汉模式:使用双检锁DCL机制
class SingletonLazySafe {
public:
	static SingletonLazySafe* GetInstance() {
		if (spts == nullptr) {
//因为spts = new SingletonLazySafe()这句指令的reorder导致执行错误,假设线程1执行到
//spts = new SingletonLazySafe();这句话,正常情况下是先调用operator new分配内存,
//再调用对象的构造器,接着把地址返回给spts,由于指令的reorder,会变成先调用operator new,
//然后将地址返回给spts,最后再调用构造器,如果线程1执行到地址返回给
//spts,时间片丢失,线程2得到执行,此时线程2使用的单例对象就是未初始化的。
			std::lock_guard lock(m_mutex);//自解锁;
			if (spts == nullptr)
				spts = new SingletonLazySafe();
			
		}
		return spts;
	}
	void test() { cout << "class lazysafe!!!" << endl; }
private:
	static mutex m_mutex;
	static SingletonLazySafe* spts;
	SingletonLazySafe() {}
	SingletonLazySafe(const SingletonLazySafe&);
	SingletonLazySafe& operator=(const SingletonLazySafe&);
};

SingletonLazySafe* SingletonLazySafe::spts = nullptr;
mutex SingletonLazySafe::m_mutex;

//饿汉模式:线程安全,在类加载时就进行初始化。
class SingletonHungary {
public:
	static SingletonHungary* GetInstance() {
		return spts;
	}
	void test() { cout << "class Hungary!!!" << endl; }
private:
	static SingletonHungary* spts;
	SingletonHungary() {}
	SingletonHungary(const SingletonHungary&);
	SingletonHungary& operator=(const SingletonHungary&);
	//内嵌一个类析构单例对象,释放资源。
	class GC {
	public:
		GC() {
			cout << "GC construction" << endl;
		}
		~GC() {
			if (spts != nullptr) {
//不在SingletonHungary本身类内执行delete释放单例对象的原因是,程序结束时系统自动释放静态变量,
//调用单例对象的析构函数,如果在单例本省的析构函数中使用delete析构单例对象的话,又会调用对象的
//析构函数,产生循环调用析构函数的情况。
				delete spts;
				spts = nullptr;
			}
			cout << "destruct" << endl;
		}
	};
	static GC gc;
};

SingletonHungary* SingletonHungary::spts = new SingletonHungary();
SingletonHungary::GC SingletonHungary::gc;

 

你可能感兴趣的:(C++)