惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
G
GRAHAM CLULEY
A
Arctic Wolf
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
Security Archives - TechRepublic
Security Archives - TechRepublic
小众软件
小众软件
S
Schneier on Security
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
美团技术团队
T
Tor Project blog
F
Full Disclosure
H
Hacker News: Front Page
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
PCI Perspectives
PCI Perspectives
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
博客园_首页
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
T
Tenable Blog
L
Lohrmann on Cybersecurity
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans
B
Blog
Last Week in AI
Last Week in AI

博客园 - 周伟

域名解析文件hosts文件是什么?如何修改hosts文件? PowerDesigner 使用心得 实现TOMCAT服务下一个ip绑定多域名绑定的方法 CSV扩展类 C++导入导出CSV文件 struct+vector实现树结构 WebService 随写 mysql相关文章收集 Linux下两种自动启动Tomcat的方法 linux自动启动mysql LINUX下mysql的大小写区分设置 Jdom使用指南 vector+struct在数据接口中的应用 UML用例中使用和扩展意义 枚举和字符串互转 单件模式的C++标准实现 设计模式 Add Crash Reporting to Your Applications with the CrashRpt Library 设计模式——单键模式(singleton)C++实现
Singleton的C++实现 及相关问题
周伟 · 2009-05-05 · via 博客园 - 周伟

编程序的时候很多情况下要求当前的程序中只有一个object。例如一个程序只有一个和数据库的连接,只有一个鼠标的object。

  最简单的方法是用个全局变量或者用个静态变量。但这违反基本的Object Oriented Design 的原则,使程序执行的整体结构,可读性以及可维护大大下降。同时如果所编写的程序不是主程序而是dll的话全局变量的寿命更难控制。

  Design Pattern 中最简单也是应用最广的就是Singleton, 就是用于解决这个问题的。下面是一个简单的Singleton的C++的实现,应用这个class之后可以保证当前程序中只有一个copy 。

Class Singleton { public: static Singleton * GetInstance() { static Singleton instance; return &instance; } protected: Singleton(); ~Singleton(); }

  由于constructor和destructor都是protected,所以无法直接生成这个class。使用时直接用 Singleton::GetInsgtance()就行了。不必操心Singleton的寿命。

  另一种实现方法如下:

Class Singleton { public: static Singleton * GetInstance() { if(!m_pInstance) m_pInstance = new Singleton(); return pinstance; } private: static Singleton *m_pInstance; protected: Singleton(); ~Singleton(); }

  这种写法的问题在于你需要在new 之后的适当时候delete 掉这个Instance。这个寿命很难控制。但有的人说这个实现是thread_safe的。
而第一个不是thread_safe 。

  我瞧了N天也没有发现这个实现怎么thread safe。经多家讨论后证明这个实现合第一个一样不thread safe。两个进程同时进入GetInstance同时m_pInstance还是NULL,同时constructor花的时间特别长的时候就可能出事。要将其用在多进程的程序中的时候最好在GetInstance函数的开始和结束加上“Cretical Section”。

  当我学完这一段的时候发现他竟然不能用在我的project里,因为我的project里要管理的这个object可能有几个copy(数量确定)。那么就需要把上面的概念稍微扩展一下。把static Singleton instance换成数组或者vector。这样能够生成的数量是确定的。使用者不会因为多用几次GetInstance而改变了内存的管理。当然用户用 GetInstance()的时候应该知道自己要Get哪个copy,给GetInstance()加个参数。稍微复杂一点。