您的位置:首页 > 编程语言 > C语言/C++

C++ 单例模式Singleton+自动释放单实例-多版本

2017-07-28 22:25 507 查看
版本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_routine
4.成功返回0  失败返回 error number

int atexit(void (*function)(void))
1.成功返回0  失败返回 error number
2.可以重复调用


#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  singleton 多线程 C++