设计模式之单体模式
来源:互联网 发布:手机免费剪辑软件 编辑:程序博客网 时间:2024/05/18 02:35
一,应用场景
全局范围内只生成一个对象,比如常见项目中的配置管理部分,通常就是一处初始化,全局使用。
单线程可以用单线程的模式,多线程就需要考虑到线程安全问题,可以借助pthread_once_t,pthread_mutex_t来实现线程安全
二,源码
包括单线程版本与线程安全版本,附简单的测试代码。
/************************************************************************* > File Name: singlton.cpp > Author:zhangtx > Mail: 18510665908@163.com > Created Time: 2015年06月06日 星期六 09时48分45秒 ************************************************************************/#include<iostream>#include<pthread.h>using namespace std;class TestC{ public: TestC() { cout<<"TestC is constructed"<<endl; } void run() { cout<<pthread_self()<<endl; }};#ifdef SINGLETHREADtemplate <class T>class Instance{ private: static T *m_value; private: Instance(){ }; ~Instance(){ }; Instance(const Instance &instance) { } Instance &operator=(const Instance &instance) { } public: static T * getInstance();};template<class T>T * Instance<T>::m_value=NULL;template<class T>T *Instance<T>::getInstance(){ if (m_value==NULL) m_value=new T(); return m_value;}int main(int argc,char *argv[]){ Instance<TestC>::getInstance(); Instance<TestC>::getInstance(); Instance<TestC>::getInstance();}#elsetemplate<class T>class Instance{ private: static T *m_value; static pthread_once_t m_once; private: Instance() { }; ~Instance() { }; Instance(const Instance &instance) { }; Instance &operator=(const Instance &instance) { } static void init() { m_value=new T(); } public: static T *getInstance();};template<class T>T * Instance<T>::m_value=NULL;template<class T>pthread_once_t Instance<T>::m_once=PTHREAD_ONCE_INIT;template<class T>T *Instance<T>::getInstance(){ pthread_once(&m_once,&init); return m_value;}void *ThreadFunc(void *){ while(true) { Instance<TestC>::getInstance()->run(); sleep(10); }}int main(int argc,char *argv[]){ int threadCount=5; pthread_t tid[5]; for(int idx=0;idx<threadCount;idx++) { pthread_create(&tid[idx],NULL,&ThreadFunc,NULL); } for(int idx=0;idx<threadCount;idx++) { pthread_join(tid[idx],NULL); } return 0;}#endif
三,运行结果
多线程场景
[root@M-192-168-10-225 algo]# g++ singleSing.cpp -lpthread[root@M-192-168-10-225 algo]# ./a.outTestC is constructed139764064061184139764053571328139764043081472139763940062976139764032591616139764053571328
139764064061184139764032591616
139763940062976139764043081472
单线程场景
[root@M-192-168-10-225 algo]# g++ singleSing.cpp [root@M-192-168-10-225 algo]# ./a.outTestC is constructed
0 0
- 设计模式之单体模式
- c#设计模式之[单体模式]
- c#设计模式之[单体模式]
- javascript设计模式之单体模式
- 设计模式------单体模式
- 设计模式之单体详解 Singleton
- 单体设计模式
- 【重读设计模式】单体模式
- JavaScript设计模式 --- 单体模式
- javascript设计模式 单体模式
- javascript 设计模式-单体模式
- JavaScript 设计模式之----单体(单例)模式
- JavaScript 设计模式之----单体(单例)模式
- javascript设计模式读书笔记(1)--单体模式
- 【js设计模式笔记---单体模式】
- 【js设计模式笔记---单体模式】
- JavaScript设计模式学习笔记:单体模式
- javascript设计模式-单体singleton模式(2)
- libxml2 使用教程
- 导入library时报错
- 堆和栈的区别
- OpenStack Live Migration (转)
- 求两个字符串的最大公共子串
- 设计模式之单体模式
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- about service in android
- C小加 之 随机数
- 舰队管理系统分析与设计-舰队管理系统分析与设计(Oracle),功能需求,数据字典,软件工程
- Kicto调查:金市消极情绪蔓延 关注希腊局势及美元走势--微微财经 贵金属直播室喊单系统 白银喊单平台 Vinv视频直播间 视频直播系统
- ICMP重定向问题的解决
- web页面邮箱的写法
- 有感于滥用多线程之遍历文件目录信息