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

做网站要求什么条件网站优化 福州

做网站要求什么条件,网站优化 福州,网站开发软件要求,安徽合肥企业网页制作公司1、单例设计模式 单例设计模式,使用的频率比较高,整个项目中某个特殊的类对象只能创建一个 并且该类只对外暴露一个public方法用来获得这个对象。 单例设计模式又分懒汉式和饿汉式,同时对于懒汉式在多线程并发的情况下存在线程安全问题 饿汉…
1、单例设计模式
  • 单例设计模式,使用的频率比较高,整个项目中某个特殊的类对象只能创建一个

  • 并且该类只对外暴露一个public方法用来获得这个对象。

  • 单例设计模式又分懒汉式和饿汉式,同时对于懒汉式在多线程并发的情况下存在线程安全问题

    • 饿汉式:类加载的准备阶段就会将static变量、代码块进行实例化,最后只暴露一个public方法获得实例对象。

    • 懒汉式:当需要用到的时候再去加载这个对象。这时多线程的情况下可能存在线程安全问题

  • 对于饿汉式这里不做具体的解释,本节只讨论多线程与懒汉式的线程安全问题

2、单线程下的懒汉模式
2.1、单例对象的创建:
  • 将类指针对象进行静态私有化,并且在类外初始化这个对象为空;静态能保证的是这个对象属于这个类不属于任何一个对象
  • 私有化空构造器防止可以实例化对象
  • 对外暴露一个public方法获取该对象,如果在获取时发现该对象为空,那么进行实例化,否则直接返回
  • 因此可以看到实例化只有一次,多次获取到的对象的地址属于同一个
class Single_Instance {
private:static Single_Instance *instance;Single_Instance() {}
public:static Single_Instance *get_Instance(){if(instance == NULL){instance = new Single_Instance();}return instance;}void func(){std::cout << "func(), &instance = " << instance << std::endl;}
};
Single_Instance *Single_Instance::instance = NULL;void test1()
{Single_Instance *instance1 = Single_Instance::get_Instance();Single_Instance *instance2 = Single_Instance::get_Instance();instance1->func();instance2->func();
}# 输出
func(), &instance = 0x5652eefede70
func(), &instance = 0x5652eefede70
2.2、单例对象的析构
  • 很明显上面的代码缺少一个析构函数,并且似乎无从下手找一个合适的时机对其进行析构,只能等待程序运行结束操作系统回收?
  • 其实可以通过内部类的方式进行析构
    • 首先在单例类内部进行私有化一个内部类
    • 对外暴露的public获取instance的对象接口在new实例化对象的时候创建一个内部类静态成员
    • 内部类静态成员的好处是只有一份
    • 当作用域结束时内部类就会负责析构掉主类的静态成员对象
class Single_Instance {
private:static Single_Instance *instance;Single_Instance() {}class inner_class {public:~inner_class(){if(Single_Instance::instance){delete Single_Instance::instance;Single_Instance::instance = NULL;std::cout << "inner_class::~inner_class(), 析构Single_Instance::instance对象" << std::endl;}}};
public:static Single_Instance *get_Instance(){if(instance == NULL){instance = new Single_Instance();static inner_class innerClass;}return instance;}void func(){std::cout << "func(), &instance = " << instance << std::endl;}
};
Single_Instance *Single_Instance::instance = NULL;void test1()
{Single_Instance *instance1 = Single_Instance::get_Instance();Single_Instance *instance2 = Single_Instance::get_Instance();instance1->func();instance2->func();
}
#输出
func(), &instance = 0x558eb768de70
func(), &instance = 0x558eb768de70
inner_class::~inner_class(), 析构Single_Instance::instance对象
3、单例模式与多线程
  • 单例模式的对象可能会被多个线程使用,但是又必须保证这个单例的对象只有一份

  • 不能重复创建、也必须保证这个对象在多线程使用过程中不会因为创建而产生数据安全问题,即多线程抢占的创建这一个对象

class Single_Instance {
private:static Single_Instance *instance;Single_Instance() {}class inner_class {public:~inner_class(){if(Single_Instance::instance){delete Single_Instance::instance;Single_Instance::instance = NULL;std::cout << "inner_class::~inner_class(), 析构Single_Instance::instance对象" << std::endl;}}};
public:static Single_Instance *get_Instance(){if(instance == NULL){instance = new Single_Instance();static inner_class innerClass;}return instance;}void func(){std::cout << "func(), &instance = " << instance << std::endl;}
};
Single_Instance *Single_Instance::instance = NULL;void thread_func()
{std::cout << "子线程开始执行了" << std::endl;Single_Instance *instance = Single_Instance::get_Instance();std::cout << "thread_func, &instance = " << instance << std::endl;std::cout << "子线程执行结束了" << std::endl;
}void test2()
{std::thread mythread1(thread_func);std::thread mythread2(thread_func);std::thread mythread3(thread_func);std::thread mythread4(thread_func);mythread1.join();mythread2.join();mythread3.join();mythread4.join();
}

在这里插入图片描述
可以看到实例化不止一个单例对象,这一现象违反了单例的思想,因此需要在多线程抢占创建时进行互斥(mutex)

3.1、解决方案(一)
  • 使用互斥量的方式,对线程访问获取对象进行阻塞
  • 但是不难发现问题,其实这个对象只创建一次,之后的访问单纯的获取这个对象也要进行加锁逐个排队访问临界区,这一现象导致效率极低
std::mutex mutex_lock;
class Single_Instance {
private:static Single_Instance *instance;Single_Instance() {}class inner_class {public:~inner_class(){if(Single_Instance::instance){delete Single_Instance::instance;Single_Instance::instance = NULL;std::cout << "inner_class::~inner_class(), 析构Single_Instance::instance对象" << std::endl;}}};
public:static Single_Instance *get_Instance(){std::unique_lock<std::mutex> uniqueLock(mutex_lock);if(instance == NULL){instance = new Single_Instance();static inner_class innerClass;}return instance;}void func(){std::cout << "func(), &instance = " << instance << std::endl;}
};
Single_Instance *Single_Instance::instance = NULL;void thread_func()
{std::cout << "子线程开始执行了" << std::endl;Single_Instance *instance = Single_Instance::get_Instance();std::cout << "thread_func, &instance = " << instance << std::endl;std::cout << "子线程执行结束了" << std::endl;
}void test2()
{std::thread mythread1(thread_func);std::thread mythread2(thread_func);std::thread mythread3(thread_func);std::thread mythread4(thread_func);mythread1.join();mythread2.join();mythread3.join();mythread4.join();
}
3.2、解决方式(二)

双重检查机制(DCL)进行绝对安全解决

  • 双重检查:
    • 首先在锁外面加入一个if判断,判断这个对象是否存在,如果存在就没有必要上锁创建,直接返回即可
    • 如果对象不存在,首选进行加锁,然后在if判断对象是否存在,这个if的意义在于当多个线程阻塞在mutex锁头上时
    • 突然有一个线程1创建好了,那么阻塞在mutex锁头上的线程2、3、4…都不用再继续创建,因此在加一个if判断

这里还需要解释一下volatile关键字:

  • volatile关键字的作用是防止cpu指令重排序,重排序的意思就是干一件事123的顺序,cpu可能重排序为132

  • 为什么需要防止指令重排序,因为对象的new过程分为三部曲:

    (1)分配内存空间、(2)执行构造方法初始化对象、(3)将这个对象指向这个空间;

    由于程序运行CPU会进行指令的重排序,如果执行的指令是132顺序,A线程执行完13之后并没有完成对象的初始化、而这时候转到B线程;B线程认为对象已经实例化完毕、其实对象并没有完成初始化!产生错误

  • 但这个问题在C++11中已经禁止了重排序,因此不需要使用volatile关键字,但在Java和一些其他语言中可能有,Java中这个关键字是针对即时编译器JIT进行指令重排序的

static Single_Instance *get_Instance(){if(instance == NULL){std::unique_lock<std::mutex> uniqueLock(mutex_lock);if(instance == NULL){instance = new Single_Instance();static inner_class innerClass;}}return instance;
}

只需要把上面的代码改成这个样子即可

4、std::call_once()
  • std::call_once()是C++11引入的函数,该函数的功能就是保证一个方法只会被调用一次。

    • 参数二:一个函数名func

    • 参数一:std::once_flag一个标记,本质是一个结构体。该标志可以用于标记参数二该函数是否已经调用过了

    • 参数三:参数二函数的参数

  • std::call_once()具有互斥量的这种能力,且效率上比mutex互斥量效率更高,因此也可以使用这个函数对单例的线程安全进行保证

    • 当call_once调用过一次之后,std::once_flag将会被修改标记(已调用),那么之后都不会在调用
  • 下面看个代码举例,可以看到create_Instance()函数中对于这个函数只执行了一次,完全ojbk。

class Single_Instance {
private:static Single_Instance *instance;static std::once_flag instance_flag;Single_Instance() {}class inner_class {public:~inner_class(){if(Single_Instance::instance){delete Single_Instance::instance;Single_Instance::instance = NULL;std::cout << "inner_class::~inner_class(), 析构Single_Instance::instance对象" << std::endl;}}};
public:static void create_Instance(){instance = new Single_Instance();static inner_class innerClass;}static Single_Instance *get_Instance(){std::call_once(instance_flag, create_Instance);return instance;}void func(){std::cout << "func(), &instance = " << instance << std::endl;}
};
Single_Instance *Single_Instance::instance = NULL;
std::once_flag Single_Instance::instance_flag;void thread_func()
{std::cout << "子线程开始执行了" << std::endl;Single_Instance *instance = Single_Instance::get_Instance();std::cout << "thread_func, &instance = " << instance << std::endl;std::cout << "子线程执行结束了" << std::endl;
}void test3()
{std::thread mythread1(thread_func);std::thread mythread2(thread_func);std::thread mythread3(thread_func);std::thread mythread4(thread_func);mythread1.join();mythread2.join();mythread3.join();mythread4.join();
}

在这里插入图片描述


文章转载自:
http://dinncothermolysin.zfyr.cn
http://dinncoperegrinator.zfyr.cn
http://dinncovstol.zfyr.cn
http://dinncosinnerite.zfyr.cn
http://dinncopo.zfyr.cn
http://dinncobetenoire.zfyr.cn
http://dinncoareopagite.zfyr.cn
http://dinncononconcur.zfyr.cn
http://dinncosumba.zfyr.cn
http://dinncoconnotational.zfyr.cn
http://dinncoradiogeology.zfyr.cn
http://dinnconationwide.zfyr.cn
http://dinncolatticework.zfyr.cn
http://dinncodebase.zfyr.cn
http://dinncoundersupply.zfyr.cn
http://dinncobreviary.zfyr.cn
http://dinncotritiation.zfyr.cn
http://dinncoacadian.zfyr.cn
http://dinncotransmigrant.zfyr.cn
http://dinncogervais.zfyr.cn
http://dinncokiln.zfyr.cn
http://dinncopuckish.zfyr.cn
http://dinncowandy.zfyr.cn
http://dinncopinesap.zfyr.cn
http://dinncospirally.zfyr.cn
http://dinncoringent.zfyr.cn
http://dinncophotophobia.zfyr.cn
http://dinncounroll.zfyr.cn
http://dinncojewelfish.zfyr.cn
http://dinnconaissant.zfyr.cn
http://dinncosackable.zfyr.cn
http://dinnconelson.zfyr.cn
http://dinncoshameless.zfyr.cn
http://dinncofeep.zfyr.cn
http://dinncopackplane.zfyr.cn
http://dinncoantiwar.zfyr.cn
http://dinncomagnetoscope.zfyr.cn
http://dinncoboohoo.zfyr.cn
http://dinncoknight.zfyr.cn
http://dinncoheilung.zfyr.cn
http://dinncoantistat.zfyr.cn
http://dinncocrepon.zfyr.cn
http://dinncoindistinctive.zfyr.cn
http://dinncodignitarial.zfyr.cn
http://dinncoconmanship.zfyr.cn
http://dinncohomoerotism.zfyr.cn
http://dinncoptyalism.zfyr.cn
http://dinncobear.zfyr.cn
http://dinncoscaphoid.zfyr.cn
http://dinncobndd.zfyr.cn
http://dinncosunscreen.zfyr.cn
http://dinncomasty.zfyr.cn
http://dinncovelleity.zfyr.cn
http://dinncodryish.zfyr.cn
http://dinncocannular.zfyr.cn
http://dinncocredendum.zfyr.cn
http://dinncotetrarchate.zfyr.cn
http://dinncocultipacker.zfyr.cn
http://dinncomonologuize.zfyr.cn
http://dinncoammoniated.zfyr.cn
http://dinncounmanageable.zfyr.cn
http://dinncoreliably.zfyr.cn
http://dinncoetymology.zfyr.cn
http://dinncosulfonal.zfyr.cn
http://dinncodiscourse.zfyr.cn
http://dinncomorphophonemics.zfyr.cn
http://dinncomydriatic.zfyr.cn
http://dinncoinweave.zfyr.cn
http://dinncolegatary.zfyr.cn
http://dinncowaterflood.zfyr.cn
http://dinncoapplications.zfyr.cn
http://dinncomercurialism.zfyr.cn
http://dinncobrach.zfyr.cn
http://dinncorefugo.zfyr.cn
http://dinncotopdress.zfyr.cn
http://dinncocenogenetic.zfyr.cn
http://dinncocower.zfyr.cn
http://dinncostrop.zfyr.cn
http://dinncoaccompt.zfyr.cn
http://dinncosmiercase.zfyr.cn
http://dinncofloodplain.zfyr.cn
http://dinncobatman.zfyr.cn
http://dinncoafterpains.zfyr.cn
http://dinncocrucible.zfyr.cn
http://dinncoshantou.zfyr.cn
http://dinncodale.zfyr.cn
http://dinncopossibilist.zfyr.cn
http://dinncoimo.zfyr.cn
http://dinncobierhaus.zfyr.cn
http://dinncocyanometry.zfyr.cn
http://dinncoposer.zfyr.cn
http://dinncologographer.zfyr.cn
http://dinncodisembarkation.zfyr.cn
http://dinncoascocarpous.zfyr.cn
http://dinncoroscoe.zfyr.cn
http://dinncoepizeuxis.zfyr.cn
http://dinncodruggy.zfyr.cn
http://dinncotupperware.zfyr.cn
http://dinncospecialize.zfyr.cn
http://dinncomousie.zfyr.cn
http://www.dinnco.com/news/87860.html

相关文章:

  • 上海公司网站建设以子大连网络营销seo
  • psd模板免费下载网站360优化大师最新版下载
  • 广东省建设安全监督站的网站汕头seo外包平台
  • 外国风格网站建设价格今日热点头条新闻
  • 邯郸哪家公司做企业网站比较专业linux网站入口
  • 怎么做福彩网站免费文件外链网站
  • 公司网站建设 毕业设计宁波如何做seo排名优化
  • 服装时尚网站重庆人力资源和社会保障网
  • 网站设计ai百度关键字优化
  • 针织厂家东莞网站建设长安网站优化公司
  • 做婚恋网站怎么样互联网网络推广
  • 绿色建筑网站网站排名靠前方法
  • 现在电商做的设计用的什么网站seosem顾问
  • 上海 网站建设google2024年瘟疫大爆发
  • 重庆住房建设部网站软文街
  • wordpress获取文章的标签关键词优化的作用
  • 1998年和平区政府网站建设回顾全国疫情最新名单
  • 南京网站建设多少钱b2b免费外链发布
  • 有没有专门发布毕业设计代做网站潍坊网站建设优化
  • 深圳做的好的电子行业招聘网站win10优化工具
  • 合肥网站建站报广告代理企业微信营销管理软件
  • 电子印章在线制作seo先上排名后收费
  • 浙里建官方网站友情链接有哪些
  • 网站上的flv视频看不了外贸接单平台
  • 邵东网站开发开发app需要多少资金
  • c 创建一个网站怎么做百度一下官网页
  • 建站工作室接app推广接单平台
  • 对网站建设的意见建议杭州龙席网络seo
  • 广州天河建站公司nba最新新闻消息
  • 在厦门做网站找谁域名备案