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

网上做电商怎么做惠州百度seo排名

网上做电商怎么做,惠州百度seo排名,无锡做网站公司有哪些,东莞设计公司排名榜单例模式&#xff0c;用于确保一个类只有一个实例&#xff0c;并提供一个全局访问点以访问该实例。 饿汉式&#xff08;Eager Initialization&#xff09; 程序启动时就创建实例 #include <iostream> class SingletonEager { private:static SingletonEager* instanc…

单例模式,用于确保一个类只有一个实例,并提供一个全局访问点以访问该实例。

饿汉式(Eager Initialization)

程序启动时就创建实例

#include <iostream>
class SingletonEager 
{
private:static SingletonEager* instance;SingletonEager() {} // 私有构造函数public:static SingletonEager* getInstance() {return instance;}
};SingletonEager* SingletonEager::instance = new SingletonEager; // 在程序启动时即创建实例int main() 
{SingletonEager* instance1 = SingletonEager::getInstance();SingletonEager* instance2 = SingletonEager::getInstance();std::cout << (instance1 == instance2) << std::endl;  // 输出 1,两个指针变量的内容相同return 0;
}

懒汉式(Lazy Initialization)

延迟初始化,即在第一次访问时才创建实例。

缺点:不是线程安全的。因为它没有考虑多线程同时访问的情况。如果多个线程同时调用 getInstance() 方法,并且在 instance 还没有被初始化之前,它们可能会同时进入条件 if (!instance) 中,导致多次创建实例,这违反了单例模式的要求。

#include <iostream>class SingletonLazy 
{
private:static SingletonLazy* instance;SingletonLazy() {} // 私有构造函数public:static SingletonLazy* getInstance() {if (!instance) {instance = new SingletonLazy;}return instance;}
};SingletonLazy* SingletonLazy::instance = nullptr;int main() {SingletonLazy* instance1 = SingletonLazy::getInstance();SingletonLazy* instance2 = SingletonLazy::getInstance();std::cout << (instance1 == instance2) << std::endl; // 输出 1,两个指针变量的内容相同return 0;
}

想要解决线程安全问题,需要做互斥操作,使用作用域互斥锁即可

class SingletonLazyThreadSafe {
private:static SingletonLazyThreadSafe* instance;static std::mutex mutex;SingletonLazyThreadSafe() {} // 私有构造函数public:static SingletonLazyThreadSafe* getInstance() {std::lock_guard<std::mutex> lock(mutex);if (!instance) {instance = new SingletonLazyThreadSafe;}return instance;}
};

变体

这种方式非常简洁,并且是线程安全的

#include <iostream>class SingletonLazy 
{
private:SingletonLazy() {} // 私有构造函数public:static SingletonLazy* getInstance() {static SingletonLazy instance;return instance;}
};SingletonLazy* SingletonLazy::instance = nullptr;int main() 
{SingletonLazy* instance1 = SingletonLazy::getInstance();SingletonLazy* instance2 = SingletonLazy::getInstance();std::cout << (instance1 == instance2) << std::endl; // 输出 1,两个指针变量的内容相同return 0;
}
http://www.yayakq.cn/news/732059/

相关文章:

  • 建工行业建设标准网站北京网站建设91086
  • 上海阀门网站建设钢结构平台
  • 代网站建设蓝色科技网站建设
  • 做外贸好的网站有哪些新乡网站建设哪家专业
  • 帝国cms获取网站地址永久免费ppt下载网站
  • 2015年网站设计慈利县建设局网站
  • 手机网站自助昆明网站建设服务
  • 广州建设银行分行招聘网站响应式网站建设的未来发展6
  • 批量上传网站产品开发app软件怎么挣钱
  • 陕西省住房和城市建设厅网站做网站的项目开发计划书
  • 政务门户网站建设方案吉林建设厅官方网站
  • 手机做网站对比路由器做网站怎么搭建自己公司网站
  • 免费制作论坛网站模板小说网站分页关键字怎么做
  • 网站开发研究背景wordpress下载软件
  • 专业模板建站做导购网站赚钱
  • wordpress支付宝移动端重庆seo综合优化
  • 东莞网站优化快速排名电商培训基地
  • 自已电脑做网站网站开发语言查询 蔡学镛
  • 网页制作教程和素材郑州官网优化推广
  • 外贸累网站做的最好的门户网站
  • 宁波做网站的公司百度指数查询平台
  • 爱站网新网址是多少网站开发费怎样入账
  • 网站分辨率做96是否会更好唐山教育平台网站建设
  • 芜湖做网站哪家好设计师设计网页大概多少钱
  • 佛山模板网站建设WordPress启用不了主题
  • 折800网站程序销售网站是什么
  • 网站更改备案外贸网站建设注意什么
  • 网站静态云南省植保植检站网址
  • 机构改革 住房与城乡建设厅网站抖音seo推广
  • 欧米伽官方网站手表价格网站设计师联盟