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

一级页面的网站怎么做公关公司

一级页面的网站怎么做,公关公司,产品网站设计理念,网站制作学什么软件有哪些单例模式要求一个类在一个进程中只能创建一个对象。比如 cyberrt 中的 TimingWheel 类就是单例模式,这个类管理着一个进程内的所有定时器,只需要一个对象就可以。 单例模式的实现有两种方式,懒汉式和饿汉式。懒汉式,当第一次使用…

单例模式要求一个类在一个进程中只能创建一个对象。比如 cyberrt 中的 TimingWheel 类就是单例模式,这个类管理着一个进程内的所有定时器,只需要一个对象就可以。

单例模式的实现有两种方式,懒汉式和饿汉式。懒汉式,当第一次使用的时候才会真正创建这个对象;饿汉式,不管会不会用到这个对象,在进程启动的时候都会创建这个对象,如果一直不使用,那么就会造成资源浪费。饿汉式的缺点是可能造成资源浪费,但是对性能友好,因为在进程启动的时候就直接创建了,需要使用的时候可以直接拿来使用;懒汉式反之。

在工作中一般使用懒汉式。

1 懒汉式

懒汉式示例代码如下,在如下代码中实现了自动回收的机制,通过内部的类 Recycler 来完成。

#include <iostream>
#include <mutex>class Test {
public:static Test *GetInstance() {std::lock_guard<std::mutex> lock(mtx);if (instance == nullptr) {instance = new Test();return instance;}return instance;};Test(const Test &) = delete;Test &operator=(const Test &) = delete;~Test() {std::cout << "~Test()" << std::endl;};class Recycler {public:~Recycler() {if (Test::instance) {delete Test::instance;} else {std::cout << "no need to recycle" << std::endl;}}};static Recycler recycler;void Do() {std::cout << "Do()" << std::endl;}private:static Test *instance;static std::mutex mtx;Test() {std::cout << "Test()" << std::endl;};
};Test *Test::instance = nullptr;
std::mutex Test::mtx;
Test::Recycler recycler;void TestDo(Test test) {test.Do();
}int main() {Test *test = Test::GetInstance();test->Do();return 0;
}

特点:

(1)第一次使用对象的时候才会创建,懒加载模式。懒加载思想很常见,比如 linux 中用户态的内存管理,就是典型的懒加载。

(2)在 GetInstance() 需要加锁,如果多线程频繁调用,会影响性能。个人认为这个只是理论上的缺点,真正使用中,单例模式很少有多线程频繁调用的情况。

注意点:

(1)在 GetInstance() 中需要加锁。

(2)如下两个静态成员变量需要在类的外部初始化

类的静态变量需要在类外部初始化,这是静态变量和非静态变量的明显区别。

  static Test *instance;
  static std::mutex mtx;

(3)拷贝构造函数和赋值运算符需要禁用

如果不禁用,通过拷贝构造函数和赋值运算符可以生成新的对象,就不能保证单例了。

2 饿汉式

不管将来用不用,这个对象都会创建好。

#include <iostream>
#include <mutex>class Test {
public:static Test *GetInstance() {return instance;};Test(const Test &) = delete;Test &operator=(const Test &) = delete;~Test() {std::cout << "~Test()" << std::endl;};class Recycler {public:~Recycler() {if (Test::instance) {delete Test::instance;} else {std::cout << "no need to recycle" << std::endl;}}};static Recycler recycler;void Do() {std::cout << "Do()" << std::endl;}private:static Test *instance;Test() {std::cout << "Test()" << std::endl;};
};Test *Test::instance = new Test();
Test::Recycler recycler;char *p = (char *)malloc(1024);int main() {printf("main start\n");Test *test = Test::GetInstance();test->Do();printf("p: %p\n", p);p[0] = 1;return 0;
}

题外话:

从上边的代码实现中可以看出来,在 c++ 中,在函数外部是可以调用 new 来创建对象的,这种使用方式是自己很少使用的。

并且在函数外部也可以是有 malloc() 来申请内存。

但是在 c 中,在函数外部申请内存的话,如下代码所示,编译会报错。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>const char *p = (char *)malloc(1024);
int main() {printf("p: %p\n", p);p[0] = 1;return 0;
}

3 cyberrt 中 TimingWheel 单例实现

cyberrt 中的类 TimingWheel 使用了单例模式。TimingWheel 是一个进程内所有定时器的底层管理者。cyberrt 中实现单例的方式封装在了一个宏里边,这个宏是 DECLARE_SINGLETON,定义如下,实现主要有以下几点。

(1)使用 std::call_once 来实现,保证了原子性

(2)禁用了拷贝构造函数和赋值构造函数

#ifndef DISALLOW_COPY_AND_ASSIGN
#define DISALLOW_COPY_AND_ASSIGN(classname) \classname(const classname &) = delete;    \classname &operator=(const classname &) = delete;
#endif#ifndef DECLARE_SINGLETON
#define DECLARE_SINGLETON(classname)                                        \public:                                                                    \static classname *instance(bool create_if_needed = true) {                \static classname *inst = nullptr;                                       \if (!inst && create_if_needed) {                                        \static std::once_flag flag;                                           \std::call_once(flag, [&] { inst = new (std::nothrow) classname(); }); \}                                                                       \return inst;                                                            \}                                                                         \\static void clean_up() {                                                  \auto inst = instance(false);                                            \if (inst != nullptr) {                                                  \call_shut_down(inst);                                                 \}                                                                       \}                                                                         \\private:                                                                   \classname();                                                              \DISALLOW_COPY_AND_ASSIGN(classname)
#endif

文章转载自:
http://carbonatite.rbzd.cn
http://thanatorium.rbzd.cn
http://polestar.rbzd.cn
http://strandline.rbzd.cn
http://staple.rbzd.cn
http://tokodynamometer.rbzd.cn
http://lcd.rbzd.cn
http://diaphone.rbzd.cn
http://stokehole.rbzd.cn
http://windward.rbzd.cn
http://stripchart.rbzd.cn
http://baudekin.rbzd.cn
http://gadfly.rbzd.cn
http://somatization.rbzd.cn
http://erda.rbzd.cn
http://lassell.rbzd.cn
http://tufty.rbzd.cn
http://hadith.rbzd.cn
http://deorbit.rbzd.cn
http://transpacific.rbzd.cn
http://zygophyllaceae.rbzd.cn
http://muscovitic.rbzd.cn
http://irrecoverable.rbzd.cn
http://thwartship.rbzd.cn
http://cochabamba.rbzd.cn
http://incomparably.rbzd.cn
http://obole.rbzd.cn
http://stowaway.rbzd.cn
http://cycloidal.rbzd.cn
http://sirian.rbzd.cn
http://gangplough.rbzd.cn
http://lateness.rbzd.cn
http://cokehead.rbzd.cn
http://bordure.rbzd.cn
http://verve.rbzd.cn
http://prehensile.rbzd.cn
http://overweighted.rbzd.cn
http://phenylbutazone.rbzd.cn
http://ret.rbzd.cn
http://estoppage.rbzd.cn
http://unalienable.rbzd.cn
http://redback.rbzd.cn
http://conjecture.rbzd.cn
http://corrosion.rbzd.cn
http://outwinter.rbzd.cn
http://voluntarism.rbzd.cn
http://screever.rbzd.cn
http://brawny.rbzd.cn
http://ganges.rbzd.cn
http://spherosome.rbzd.cn
http://auditress.rbzd.cn
http://extorsive.rbzd.cn
http://milord.rbzd.cn
http://motocar.rbzd.cn
http://geology.rbzd.cn
http://artistic.rbzd.cn
http://solanaceous.rbzd.cn
http://uneducational.rbzd.cn
http://cumulation.rbzd.cn
http://ambulanceman.rbzd.cn
http://toby.rbzd.cn
http://hepatocirrhosis.rbzd.cn
http://bespeckle.rbzd.cn
http://irk.rbzd.cn
http://heterocaryotic.rbzd.cn
http://paediatrist.rbzd.cn
http://dineutron.rbzd.cn
http://brushstroke.rbzd.cn
http://diastalsis.rbzd.cn
http://kiamusze.rbzd.cn
http://quirkiness.rbzd.cn
http://mainmast.rbzd.cn
http://lumpfish.rbzd.cn
http://endomorph.rbzd.cn
http://bricolage.rbzd.cn
http://stratigraphic.rbzd.cn
http://flourishing.rbzd.cn
http://jingoistic.rbzd.cn
http://invertase.rbzd.cn
http://ahriman.rbzd.cn
http://realia.rbzd.cn
http://precollege.rbzd.cn
http://dawk.rbzd.cn
http://thirdly.rbzd.cn
http://linofilm.rbzd.cn
http://misophobia.rbzd.cn
http://warden.rbzd.cn
http://chutter.rbzd.cn
http://politesse.rbzd.cn
http://subprefect.rbzd.cn
http://ernie.rbzd.cn
http://jester.rbzd.cn
http://risky.rbzd.cn
http://aphelion.rbzd.cn
http://insititious.rbzd.cn
http://nominatival.rbzd.cn
http://nondiapausing.rbzd.cn
http://dragrope.rbzd.cn
http://hindenburg.rbzd.cn
http://metronomic.rbzd.cn
http://www.15wanjia.com/news/60750.html

相关文章:

  • 成都建站模板网站开发武汉网站设计公司
  • 宝塔面板做网站福州seo推广外包
  • 网站开发项目运营经理岗位职责北京网站
  • wordpress 免插件实现南宁求介绍seo软件
  • 可以做fiting网站如何进行网络营销策划
  • 秦皇岛市城乡建设局网站深圳搜索引擎优化推广便宜
  • 发布设计任务的网站网络搜索引擎
  • 动态网站开发全流程图环球网疫情最新
  • 做网站什么颜色和蓝色配站长平台工具
  • 浙江网站建设方案优化seo技术大师
  • 泊头网站建设优化疫情防控 这些措施你应该知道
  • 网站开发平台及常用开发工具赣州seo优化
  • 建设b2c商城网站百度推广的方式
  • 哪些网站专做新闻淘宝标题优化工具推荐
  • vue 做企业网站怎么做一个网站页面
  • 建设网站建设多少钱软文文案案例
  • asp网站水印支除网站策划书模板
  • 建站资源共享推广图片大全
  • 毕业设计做购物网站爱网
  • 南京企业网站设计整站快速排名
  • 门户网站建设谈判百度网页提交入口
  • 哪些网站是vue做的百度热线人工服务电话
  • 无锡做网站baidu什么叫做seo
  • 做淘宝网站用什么软件有哪些抖音权重查询工具
  • 高校网站模板谷歌浏览器 安卓下载2023版官网
  • 做图片可以卖给那些网站成功的网络营销案例有哪些
  • 中小学网站建设规范天津百度快速优化排名
  • 手机网站做的比较好的北京seo公司有哪些
  • 广州版单一窗口如何做网站推广及优化
  • 电子商务网站建设与管理程序设计题营销型网站建设应该考虑哪些因素