博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单例模式(C++实现)
阅读量:4125 次
发布时间:2019-05-25

本文共 2627 字,大约阅读时间需要 8 分钟。

原文链接: or

1. 单例模式 (C++实现)

单例模式:保证一个类只有一个对象实例,并提供一个访问该对象实例的全局访问点。

单例模式有两种实现方法:懒汉模式和饿汉模式。

1.1 懒汉模式

懒汉模式:故名思义,不到万不得已就不会去实例化类,也就是说在第一次用到类实例时候才会去实例化它。

在访问量较小时,采用懒汉模式,这里是以时间换空间。

线程安全的懒汉实现:

Singleton.cpp

#include 
#include
#include
using namespace std;class Singleton;class Singleton {
private: static Singleton *gInstance; static pthread_mutex_t g_tMutex;public: static Singleton *getInstance() {
if (NULL == gInstance)//双重锁定,提高多线程性能 {
pthread_mutex_lock(&g_tMutex); if (NULL == gInstance) gInstance = new Singleton; pthread_mutex_unlock(&g_tMutex); } return gInstance; } void printInfo(){
cout<<"This is singleton"<
printInfo(); return NULL;}void *start_routine_thread2(void *arg){
cout<<"this is thread 2 ..."<
printInfo(); return NULL;}int main(){
Singleton *s = Singleton::getInstance(); s->printInfo(); Singleton *s2 = Singleton::getInstance(); s2->printInfo(); Singleton *s3 = Singleton::getInstance(); s3->printInfo(); /* 创建线程,在线程里也去调用Singleton::getInstance */ pthread_t thread1ID; pthread_t thread2ID; pthread_create(&thread1ID, NULL, start_routine_thread1, NULL); pthread_create(&thread2ID, NULL, start_routine_thread2, NULL); sleep(3); return 0;}

测试:

$ g++ Singleton.cpp -pthread$ ./a.outSingleton()This is singletonThis is singletonThis is singletonthis is thread 1 ...This is singletonthis is thread 2 ...This is singleton

1.2 饿汉模式

饿汉模式:饿了肯定要饥不择食,所以在单例类定义的时候就进行实例化。

在访问的线程比较多时,采用饿汉模式,可以实现更好的性能,这里是以空间换时间。饿汉模式线程是安全的,因为一开始已经对单例类进行的实例化。

饿汉模式的实现:

Singleton2.cpp

#include 
#include
#include
using namespace std;class Singleton;class Singleton {
private: static Singleton *gInstance;public: static Singleton *getInstance() {
return gInstance; } void printInfo(){
cout<<"This is singleton"<
printInfo(); return NULL;}void *start_routine_thread2(void *arg){
cout<<"this is thread 2 ..."<
printInfo(); return NULL;}int main(){
Singleton *s = Singleton::getInstance(); s->printInfo(); Singleton *s2 = Singleton::getInstance(); s2->printInfo(); Singleton *s3 = Singleton::getInstance(); s3->printInfo(); /* 创建线程,在线程里也去调用Singleton::getInstance */ pthread_t thread1ID; pthread_t thread2ID; pthread_create(&thread1ID, NULL, start_routine_thread1, NULL); pthread_create(&thread2ID, NULL, start_routine_thread2, NULL); sleep(3); return 0;}

调试:

$ g++ Singleton2.cpp -pthread$ ./a.outSingleton()This is singletonThis is singletonThis is singletonthis is thread 1 ...this is thread 2 ...This is singletonThis is singleton

转载地址:http://pglpi.baihongyu.com/

你可能感兴趣的文章
Ubuntu 16.04 apt-get更换为国内阿里云源
查看>>
laravel部署到宝塔步骤
查看>>
小程序获取access_token
查看>>
navicat远程连接mysql数据库
查看>>
tp5令牌数据无效 解决方法
查看>>
自己的网站与UCenter整合(大致流程)
查看>>
laravel 制作通用的curd 后台操作
查看>>
【小红书2017年笔试】求一个数组中平均数最大的子数组
查看>>
Linux基础系列-定时器与时间管理
查看>>
Linux基础系列-可执行程序的产生过程
查看>>
Linux基础系列-Kernel 初始化宏
查看>>
Linux子系统系列-I2C
查看>>
<iOS>关于自定义description的一点用法
查看>>
Unix 命令,常用到的
查看>>
DLL中建立进程共享数据段需要注意的语法问题
查看>>
服务器端技术----Http请求的处理过程
查看>>
C语言-预处理指令2-条件编译
查看>>
C语言-预处理指令3-文件包含
查看>>
C语言-变量类型
查看>>
C语言-static和extern关键字1-对函数的作用
查看>>