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

推荐订阅源

Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
A
About on SuperTechFans
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Webroot Blog
Webroot Blog
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
Simon Willison's Weblog
Simon Willison's Weblog
D
Docker
爱范儿
爱范儿
AI
AI
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
The Register - Security
The Register - Security
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
S
Securelist
Scott Helme
Scott Helme
B
Blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
月光博客
月光博客
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs
H
Help Net Security
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
I
InfoQ
P
Privacy International News Feed
V
V2EX
有赞技术团队
有赞技术团队
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Schneier on Security
T
Tailwind CSS Blog

博客园 - 周伟

域名解析文件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()加个参数。稍微复杂一点。