Singleton in C++11 style
#include <iostream>
#include <memory>
#include <mutex> class SingletonOld {
static std::shared_ptr<SingletonOld> handle_;
static std::mutex handle_mutex_; int data_ = 0; public:
static auto create() {
std::lock_guard<std::mutex> lock(handle_mutex_);
if (handle_ == nullptr) {
handle_.reset(new SingletonOld); // Using `make_shared` is error, as `SingletonOld` constructor is private
// to `shared_ptr`, `allocator` and any class else.
// handle_ = std::make_shared<SingletonOld>();
}
return handle_;
} int get_data() { return data_; }
void set_data(int data) { data_ = data; } private:
SingletonOld(const SingletonOld &) = delete;
SingletonOld &operator=(const SingletonOld &) = delete; SingletonOld() {};
}; std::shared_ptr<SingletonOld> SingletonOld::handle_;
std::mutex SingletonOld::handle_mutex_; class Singleton {
int data_ = 0; public:
static Singleton &create() {
// 1. C++11: If control enters the declaration concurrently while the
// variable is being initialized, the concurrent execution shall wait for
// completion of the initialization.
// 2. Lazy evaluation.
static Singleton s; return s;
} int get_data() { return data_; }
void set_data(int data) { data_ = data; } private:
Singleton(const Singleton &) = delete;
Singleton &operator=(const Singleton &) = delete; Singleton() {}
}; int main() {
auto p = SingletonOld::create();
std::cout << p->get_data() << std::endl; // 0
p->set_data(1);
auto q = p;
std::cout << q->get_data() << std::endl; // 1 Singleton &s = Singleton::create();
Singleton &r = s;
r.get_data();
std::cout << s.get_data() << std::endl; // 0
s.set_data(1);
std::cout << r.get_data() << std::endl; // 1 return 0;
}
Singleton in C++11 style的更多相关文章
- Google C++ Style Guide在C++11普及后的变化
转 http://www.cnblogs.com/chen3feng/p/5972967.html?from=timeline&isappinstalled=0&lwfrom=user ...
- IE10 11的css hack
一.@media -ms-high-contrast @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none){ ...
- .NET设计模式(2):单件模式(Singleton Pattern)
转载:http://terrylee.cnblogs.com/archive/2005/12/09/293509.html 单件模式(Singleton Pattern) --.NET设计模式系列之二 ...
- C++ 11和C++98相比有哪些新特性
此文是如下博文的翻译: https://herbsutter.com/elements-of-modern-c-style/ C++11标准提供了许多有用的新特性.这篇文章特别针对使C++11和C++ ...
- C++11:使用 auto/decltype/result_of使代码可读易维护
C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...
- C++闭包: Lambda Functions in C++11
表达式无疑是C++11最激动人心的特性之一!它会使你编写的代码变得更优雅.更快速! 它实现了C++11对于支持闭包的支持.首先我们先看一下什么叫做闭包 维基百科上,对于闭包的解释是: In progr ...
- [转载]如何在C++03中模拟C++11的右值引用std::move特性
本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...
- Singleton(单例)
意图: 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 适用性: 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时. 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代 ...
- C++中多线程与Singleton的那些事儿
前言 前段时间在网上看到了个的面试题,大概意思是如何在不使用锁和C++11的情况下,用C++实现线程安全的Singleton. 看到这个题目后,第一个想法就是用Scott Meyer在<Effe ...
随机推荐
- Arch Linux PDF格式文件无法显示中文
From: http://blog.sina.com.cn/s/blog_5e54bc6801012gfg.html $ sudo pacman -S poppler-data
- 编译WebRTC遇到的问题总结
唉,本人下载WebRTC的代码都用了几天,真的是惭愧,本来以为很简单的东西,没想到搞了这么久,在下载的过程中,心里骂了无数遍XXX,这鬼东西咋这么难搞.后来终于搞明白了为啥代码总是下载不了,然后又在心 ...
- 关于计算机的ID和用户ID之间的关系
关于计算机的ID和用户ID之间的关系 计算机安装完系统后就会生成计算机ID,然后系统会以计算机ID为前缀附加数字创建Administrator(500)和Guest(501)用户ID,其他用户的ID将 ...
- JavaScript笔记基础篇(三)
针对前段JS获取当前时间或者对时间数据处理方法汇总: javascript 字符串转化为日期 Java代码 var s="2010-5-18 12:30:20"; var t= ...
- javaEE中关于dao层和services层的理解
javaEE中关于dao层和services层的理解 入职已经一个多月了,作为刚毕业的新人,除了熟悉公司的项目,学习公司的框架,了解项目的一些业务逻辑之外,也就在没学到什么:因为刚入职, 带我的那个师 ...
- js的事件的绑定
js的绑定事件 绑定事件有什么用,当你一个按钮点击时需要执行2个函数,就可以用绑定事件,一般只有没绑定事件增加两个onclick函数,第二会覆盖第一个函数的. <!DOCTYPE html> ...
- C#实现Dll(OCX)控件自动注册的两种方法 网上找的 然后 自己试了试 还是可以用的
尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...
- [转]DevExpress.XtraGrid.GridControl gridView自动列宽代码
gridView1.OptionsView.ColumnAutoWidth = false; for (int I = 0; I < gridView1.Columns.Count; I++ ...
- loadrunner录制脚本如何选择使用get请求和post请求的方式
在loadrunner工具里录制脚本时常常会用到get请求和post请求,有关loadrunner常用的这两类的请求主要有: get请求: web_url 和 web_link post请求: web ...
- Product Backlog
会议时间:周四中午13:20-14:20 会议地点:寝室 讨论了如何根据用户故事来写排球的程序.如何实现单机模式的基本加分判断胜负的功能.并选出项目测试人员.然后两个人一个小组进行程序基本功能的开发. ...