C++ 单例模式Singleton+自动释放单实例-多版本
来源:互联网 发布:淘宝店铺收到差评 编辑:程序博客网 时间:2024/06/14 08:18
版本1:
pthread_once 保证多线程环境下懒汉单例模式安全
atexit 注册程序结束时的销毁函数,自动销毁单实例
int pthread_once(pthread_once_t * once_control , void(* init_routine)(void)); 1.其中pthread_once_t once_control = PTHREAD_ONCE_INIT;2.init_routine是函数指针 3.使用相同的once_control的pthread_once() 的后续调用不能调用init_routine4.成功返回0 失败返回 error numberint atexit(void (*function)(void))1.成功返回0 失败返回 error number2.可以重复调用
#include <stdlib.h>#include <pthread.h>#include <iostream>using std::cout;using std::endl;class Singleton{public: static Singleton * getInstance() { pthread_once(&_once_control, init_routine); //静态成员函数只能使用静态成员,故_once_control、init_routine函数指针为static return _pInstance; } static void init_routine() //多线程环境仍然能保证仅执行一次 -> 创建单实例对象+注册atexit函数 { _pInstance = new Singleton; atexit(destroy); //注册退出后执行的函数destory, 实现单实例的自动释放 } static void destroy() { if(_pInstance) delete _pInstance; }private: Singleton(){ cout << "Singleton()" << endl; } ~Singleton(){ cout << "~Singleton()" << endl; }private: static Singleton * _pInstance; static pthread_once_t _once_control;};Singleton * Singleton::_pInstance = NULL;pthread_once_t Singleton::_once_control = PTHREAD_ONCE_INIT;int main(void){ //*****************单例模式测试 Singleton *s1=Singleton::getInstance(); Singleton *s2=Singleton::getInstance(); Singleton *s3=Singleton::getInstance(); cout<<"addr: "<<s1<<endl; cout<<"addr: "<<s2<<endl; cout<<"addr: "<<s3<<endl; return 0;}
版本2:
饱汉模式+静态嵌套类对象
静态类对象位于全局区,当程序结束时调用嵌套类析构函数,实现单例模式中实例自动销毁
#include <iostream>using std::cout;using std::endl;// 静态的嵌套类对象(保存在全局区) 实现单实例的自动释放class Singleton{private: class AutoRelease { public: AutoRelease() { cout << "AutoRelease()" << endl; } ~AutoRelease() { cout << "~AutoRelease()" << endl; if(_pInstance) delete _pInstance; //嵌套类内部 释放单实例 } };public: //懒汉模式在多线程环境下并不是线程安全的 static Singleton * getInstance() { if(NULL == _pInstance) _pInstance = new Singleton; return _pInstance; }private: Singleton(){ cout << "Singleton()" << endl; } ~Singleton(){ cout << "~Singleton()" << endl; }private: static Singleton * _pInstance; static AutoRelease _auto;};//懒汉式/饿汉式//Singleton * Singleton::_pInstance = NULL;//懒加载Singleton * Singleton::_pInstance = getInstance();//饱汉式, 可以保证是线程安全的 //缺点:该对象会一直存在,即使在当前没有立即使用Singleton::AutoRelease Singleton::_auto; //静态的嵌套类对象int main(void){ Singleton::getInstance(); return 0;}
版本3:
atexit + 饱汉式
atexit函数用于程序退出后的回调函数
int atexit(void (*function)(void))
#include <stdlib.h>#include <iostream>using std::cout;using std::endl;class Singleton{public: static Singleton * getInstance() { if(NULL == _pInstance) { _pInstance = new Singleton; atexit(destroy); } return _pInstance; } static void destroy() { if(_pInstance) delete _pInstance; }private: Singleton(){ cout << "Singleton()" << endl; } ~Singleton(){ cout << "~Singleton()" << endl; }private: static Singleton * _pInstance;};Singleton * Singleton::_pInstance = getInstance();//饱汉式, 可以保证是线程安全的 //缺点:该对象会一直存在,即使在当前没有立即使用int main(void){ Singleton::getInstance(); return 0;}
阅读全文
0 0
- C++ 单例模式Singleton+自动释放单实例-多版本
- C++单例模式(自动释放实例)
- 单实例模式(Singleton)
- c++-单例模式Singleton
- 设计模式--单例(singleton)实例
- 单实例设计模式(Singleton)
- 单实例Singleton设计模式
- Objective C 实现Singleton(单例)模式.
- Objective —C 单例模式 Singleton
- Objective-C使用单例(Singleton)模式
- 【Object-C】单例模式Singleton
- Object-C单例模式Singleton
- Objective-C中的单例模式(singleton)
- c++--Singleton单例模式的实现
- Swift、Objective-C 单例模式 (Singleton)
- 单例(Singleton)模式
- singleton 单例模式
- Singleton单例模式
- 205. Isomorphic Strings
- RH134-第十五节-PXE自动化系统批量安转以及PXE环境搭建
- 详解--并查集【转载】
- Java发送邮件,MailUtil公共类
- webview系列:Html5页面和Native App怎么进行交互
- C++ 单例模式Singleton+自动释放单实例-多版本
- 每日一题之判断一个节点是否在一棵二叉树中
- 大一回顾【C语言】
- MyEclipse10构建Maven项目总结
- 7.28学习心得
- 空格替换
- c语言实现静态顺序表
- 继承
- poj2752 Seek the Name, Seek the Fame(kmp)