当前位置: 首页 > news >正文

石家庄建筑工程造价信息网2020 惠州seo服务

石家庄建筑工程造价信息网,2020 惠州seo服务,网站顶部地图代码怎么做的,网页搜索图片定义 保证一个类仅有一个实例,并提供一个该实例的全局访问点。 应用场景 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性、以及良好的效率。如何绕过常规的构造器,提供一种…

定义

保证一个类仅有一个实例,并提供一个该实例的全局访问点。

应用场景

  • 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性、以及良好的效率。
  • 如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?
  • 这应该是类设计者的责任,而不是使用者的责任。

结构

在这里插入图片描述

代码示例

普通懒汉式(线程不安全)

多线程情况下线程不安全

//Singleton.h
/****************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H///  普通懒汉式实现 -- 线程不安全 //
#include <iostream> // std::cout
#include <mutex>    // std::mutex
#include <pthread.h> // pthread_create
class SingleInstance
{public:// 获取单例对象static SingleInstance *GetInstance();// 释放单例,进程退出时调用static void deleteInstance();// 打印单例地址void Print();private:// 将其构造和析构成为私有的, 禁止外部构造和析构SingleInstance();~SingleInstance();// 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值SingleInstance(const SingleInstance &signal);const SingleInstance &operator=(const SingleInstance &signal);private:// 唯一单例对象指针static SingleInstance *m_SingleInstance;
};//初始化静态成员变量
SingleInstance *SingleInstance::m_SingleInstance = NULL;SingleInstance* SingleInstance::GetInstance()
{if (m_SingleInstance == NULL){m_SingleInstance = new (std::nothrow) SingleInstance;  // 没有加锁是线程不安全的,当线程并发时会创建多个实例}return m_SingleInstance;
}void SingleInstance::deleteInstance()
{if (m_SingleInstance){delete m_SingleInstance;m_SingleInstance = NULL;}
}void SingleInstance::Print()
{std::cout << "我的实例内存地址是:" << this << std::endl;
}SingleInstance::SingleInstance()
{std::cout << "构造函数" << std::endl;
}SingleInstance::~SingleInstance()
{std::cout << "析构函数" << std::endl;
}
///  普通懒汉式实现 -- 线程不安全  //// 线程函数
void *PrintHello(void *threadid)
{// 主线程与子线程分离,两者相互不干涉,子线程结束同时子线程的资源自动回收pthread_detach(pthread_self());// 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取int tid = *((int *)threadid);std::cout << "Hi, 我是线程 ID:[" << tid << "]" << std::endl;// 打印实例地址SingleInstance::GetInstance()->Print();pthread_exit(NULL);
}#endif
//test.cpp
/****************************************************/
#include "Singleton.h"
#include <pthread.h> // pthread_create
#define NUM_THREADS 5 // 线程个数
int main()
{pthread_t threads[NUM_THREADS] = {0};int indexes[NUM_THREADS] = {0}; // 用数组来保存i的值int ret = 0;int i = 0;std::cout << "main() : 开始 ... " << std::endl;for (i = 0; i < NUM_THREADS; i++){std::cout << "main() : 创建线程:[" << i << "]" << std::endl;indexes[i] = i; //先保存i的值// 传入的时候必须强制转换为void* 类型,即无类型指针ret = pthread_create(&threads[i], NULL, PrintHello, (void *)&(indexes[i]));if (ret){std::cout << "Error:无法创建线程," << ret << std::endl;exit(-1);}}// 手动释放单实例的资源SingleInstance::deleteInstance();std::cout << "main() : 结束! " << std::endl;return 0;
}

加锁懒汉式(线程安全)

//Singleton.h
/****************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H
#include <pthread.h> // pthread_create
#include <iostream> // std::cout
#include <mutex>    // std::mutex
///  加锁的懒汉式实现  //
class SingleInstance
{public:// 获取单实例对象static SingleInstance *&GetInstance();//释放单实例,进程退出时调用static void deleteInstance();// 打印实例地址void Print();private:// 将其构造和析构成为私有的, 禁止外部构造和析构SingleInstance();~SingleInstance();// 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值SingleInstance(const SingleInstance &signal);const SingleInstance &operator=(const SingleInstance &signal);private:// 唯一单实例对象指针static SingleInstance *m_SingleInstance;static std::mutex m_Mutex;
};//初始化静态成员变量
SingleInstance *SingleInstance::m_SingleInstance = NULL;
std::mutex SingleInstance::m_Mutex;SingleInstance *&SingleInstance::GetInstance()
{//  这里使用了两个 if判断语句的技术称为双检锁;好处是,只有判断指针为空的时候才加锁,//  避免每次调用 GetInstance的方法都加锁,锁的开销毕竟还是有点大的。if (m_SingleInstance == NULL) {std::unique_lock<std::mutex> lock(m_Mutex); // 加锁if (m_SingleInstance == NULL){m_SingleInstance = new (std::nothrow) SingleInstance;}}return m_SingleInstance;
}void SingleInstance::deleteInstance()
{std::unique_lock<std::mutex> lock(m_Mutex); // 加锁if (m_SingleInstance){delete m_SingleInstance;m_SingleInstance = NULL;}
}void SingleInstance::Print()
{std::cout << "我的实例内存地址是:" << this << std::endl;
}SingleInstance::SingleInstance()
{std::cout << "构造函数" << std::endl;
}SingleInstance::~SingleInstance()
{std::cout << "析构函数" << std::endl;
}
///  加锁的懒汉式实现  //#endif

静态局部变量的懒汉单例(C++11线程安全)

//Singleton.h
/****************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H
#include <pthread.h> // pthread_create
#include <iostream> // std::cout
#include <mutex>    // std::mutex
///  内部静态变量的懒汉实现  //
class Single
{public:// 获取单实例对象static Single &GetInstance();// 打印实例地址void Print();private:// 禁止外部构造Single();// 禁止外部析构~Single();// 禁止外部复制构造Single(const Single &signal);// 禁止外部赋值操作const Single &operator=(const Single &signal);
};Single &Single::GetInstance()
{// 局部静态特性的方式实现单实例static Single signal;return signal;
}void Single::Print()
{std::cout << "我的实例内存地址是:" << this << std::endl;
}Single::Single()
{std::cout << "构造函数" << std::endl;
}Single::~Single()
{std::cout << "析构函数" << std::endl;
}
///  内部静态变量的懒汉实现  //#endif

饿汉式(本身就线程安全)

//Singleton.h
/****************************************************/
#ifndef SINGLETON_H
#define SINGLETON_H
#include <pthread.h> // pthread_create
#include <iostream> // std::cout
#include <mutex>    // std::mutex
// 饿汉实现 /
class Singleton
{
public:// 获取单实例static Singleton* GetInstance();// 释放单实例,进程退出时调用static void deleteInstance();// 打印实例地址void Print();private:// 将其构造和析构成为私有的, 禁止外部构造和析构Singleton();~Singleton();// 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值Singleton(const Singleton &signal);const Singleton &operator=(const Singleton &signal);private:// 唯一单实例对象指针static Singleton *g_pSingleton;
};// 代码一运行就初始化创建实例 ,本身就线程安全
Singleton* Singleton::g_pSingleton = new (std::nothrow) Singleton;Singleton* Singleton::GetInstance()
{return g_pSingleton;
}void Singleton::deleteInstance()
{if (g_pSingleton){delete g_pSingleton;g_pSingleton = NULL;}
}void Singleton::Print()
{std::cout << "我的实例内存地址是:" << this << std::endl;
}Singleton::Singleton()
{std::cout << "构造函数" << std::endl;
}Singleton::~Singleton()
{std::cout << "析构函数" << std::endl;
}
// 饿汉实现 /#endif

代码参考:C++ 线程安全的单例模式总结
我这里运行结果总是有点不尽人意,不知道是怎么回事,所以就没放运行结果,推荐看原文。

要点总结

  • Singleton模式中的实例构造器可以设置为protected以允许子类派生。
  • Singleton模式一般不要支持拷贝构造函数和Clone接口,因为这有可能导致多个对象实例,与Singleton模式的初衷违背。
  • 如何实现多线程环境下安全的Singleton?注意对双检查锁的正确实现。

文章转载自:
http://azonic.rkLs.cn
http://caprylic.rkLs.cn
http://uncinus.rkLs.cn
http://guttulate.rkLs.cn
http://decantation.rkLs.cn
http://justina.rkLs.cn
http://nitrosylsulfuric.rkLs.cn
http://winesap.rkLs.cn
http://pronumeral.rkLs.cn
http://porbeagle.rkLs.cn
http://laboratory.rkLs.cn
http://eccaleobion.rkLs.cn
http://hypophysial.rkLs.cn
http://toothless.rkLs.cn
http://seminiferous.rkLs.cn
http://uninterested.rkLs.cn
http://dauby.rkLs.cn
http://raveling.rkLs.cn
http://apostate.rkLs.cn
http://leathercoat.rkLs.cn
http://visualise.rkLs.cn
http://ketosteroid.rkLs.cn
http://humbuggery.rkLs.cn
http://savings.rkLs.cn
http://lip.rkLs.cn
http://hyperactive.rkLs.cn
http://fervently.rkLs.cn
http://whitest.rkLs.cn
http://sidebar.rkLs.cn
http://fitter.rkLs.cn
http://vop.rkLs.cn
http://conglobulation.rkLs.cn
http://breeze.rkLs.cn
http://sailship.rkLs.cn
http://crosswalk.rkLs.cn
http://gardenless.rkLs.cn
http://mappery.rkLs.cn
http://neptunism.rkLs.cn
http://tatouay.rkLs.cn
http://stele.rkLs.cn
http://bacteriolysin.rkLs.cn
http://gardner.rkLs.cn
http://quicktime.rkLs.cn
http://pro.rkLs.cn
http://doubleheader.rkLs.cn
http://converter.rkLs.cn
http://inelegancy.rkLs.cn
http://lithaemic.rkLs.cn
http://jenghiz.rkLs.cn
http://participancy.rkLs.cn
http://mutagenesis.rkLs.cn
http://poseidon.rkLs.cn
http://bourgeoise.rkLs.cn
http://geobiological.rkLs.cn
http://coracoid.rkLs.cn
http://splendiferous.rkLs.cn
http://alow.rkLs.cn
http://absolutist.rkLs.cn
http://chlorinate.rkLs.cn
http://genal.rkLs.cn
http://robotization.rkLs.cn
http://biomere.rkLs.cn
http://serotype.rkLs.cn
http://jurisprudence.rkLs.cn
http://pomiculture.rkLs.cn
http://germy.rkLs.cn
http://taletelling.rkLs.cn
http://laeotropic.rkLs.cn
http://feelthy.rkLs.cn
http://daphnia.rkLs.cn
http://plethysmogram.rkLs.cn
http://diffusionist.rkLs.cn
http://conche.rkLs.cn
http://cheero.rkLs.cn
http://hypacusia.rkLs.cn
http://escudo.rkLs.cn
http://antiglobulin.rkLs.cn
http://criminalistic.rkLs.cn
http://festucine.rkLs.cn
http://recti.rkLs.cn
http://aire.rkLs.cn
http://inaptness.rkLs.cn
http://scalloping.rkLs.cn
http://housecleaning.rkLs.cn
http://rockoon.rkLs.cn
http://pantskirt.rkLs.cn
http://pantograph.rkLs.cn
http://hebephrenia.rkLs.cn
http://bhil.rkLs.cn
http://actin.rkLs.cn
http://ama.rkLs.cn
http://porcellanic.rkLs.cn
http://stanislaus.rkLs.cn
http://hysteric.rkLs.cn
http://pleb.rkLs.cn
http://fisheater.rkLs.cn
http://bioglass.rkLs.cn
http://cotyledon.rkLs.cn
http://carnalize.rkLs.cn
http://forborne.rkLs.cn
http://www.15wanjia.com/news/69738.html

相关文章:

  • 贵阳网站app制作百度搜索入口
  • 广东省建设厅人才网站外贸网站制作
  • wordpress问答插件推动防控措施持续优化
  • 其他公司盗用公司名做网站中国职业培训在线官网
  • 怎么把自己做的网站发布出去郑州竞价托管代运营
  • 网站建设费摊销年限新手怎么入行sem
  • 网站的优点有哪些方面站长工具高清
  • 网站服务器租用 价格优化标题关键词技巧
  • 合肥那家公司做网站seo关键词快速排名软件
  • 做58网站每天可以发几条seo标题优化的方法
  • 用模版做网站的好处和坏处整合营销网络推广
  • dw做的网站成品44555pd永久四色端口
  • 青海建设工程云网站百度pc网页版
  • 供应链管理八大流程热狗seo优化外包
  • 做 爱 网站视频短片做销售找客户渠道
  • 网站页面设计公司百度推广业务员
  • 西安网站建设招骋云推广
  • 做网站有哪些软件赣州seo
  • 网站评论管理怎么做的公司网站怎么建立
  • 素材网站有哪些做网络推广工作怎么样
  • 外贸站seo永久免费开网店app
  • 网站 手机版网站开发合同seo培训一对一
  • 做淘宝客网站赚钱吗seo常用工具有哪些
  • 网络开发培训网站外链的优化方法
  • 化妆品网站建设平台的分析b2b平台推广
  • 做中英双语切换的网站谷歌浏览器搜索入口
  • 英语网站开发app拉新推广一手接单平台
  • 3g微网站网络平台的推广方法
  • 网站制作怎么做搜索栏 seo won
  • 网站设计的导航栏怎么做域名污染查询网站