Singleton [Double-Checked Locling] 双重加琐优化
来源:互联网 发布:唯一网络王宇杰祖籍 编辑:程序博客网 时间:2024/05/24 11:13
Double-Checked Locking实现:
同步类:
synobj.h
1 #ifndef SYNOBJ_H
2 #define SYNOBJ_H
3
4 #include <windows.h>
5
6 #define CLASS_UNCOPYABLE(classname) /
7 private: /
8 classname##(const classname##&); /
9 classname##& operator=(const classname##&);
10
11 class Mutex {
12 CLASS_UNCOPYABLE(Mutex)
13 public:
14 Mutex() :_cs() { InitializeCriticalSection(&_cs); }
15 ~Mutex() { DeleteCriticalSection(&_cs); }
16 void lock() { EnterCriticalSection(&_cs); }
17 void unlock() { LeaveCriticalSection(&_cs); }
18 private:
19 CRITICAL_SECTION _cs;
20 };
21
22 class Lock {
23 CLASS_UNCOPYABLE(Lock)
24 public:
25 explicit Lock(Mutex& cs) :_cs(cs) { _cs.lock(); }
26 ~Lock() { _cs.unlock(); }
27 private:
28 Mutex& _cs;
29 };
30
31 #endif/*SYNOBJ_H*/
有了同步对象很容易就能够写出如下代码:
singleton.h
1 #ifndef SINGLETON_H
2 #define SINGLETON_H
3
4 #include "synobj.h"
5
6 class Singleton {
7 public:
8 static Singleton& Instance() { // Unique point of access
If ( 0 == _instance)
{ // 因为lock只对 0 == _instance为true才有效,所以为了优化就再加入了一个判断。
9 Lock lock(_mutex);
10 if (0 == _instance) {
11 _instance = new Singleton();
12 atexit(Destroy); // Register Destroy function
13 }
}
14 return *_instance;
15 }
16 void DoSomething(){}
17 private:
18 static void Destroy() { // Destroy the only instance
19 if ( _instance != 0 ) {
20 delete _instance;
21 _instance = 0;
22 }
23 }
24 Singleton(){} // Prevent clients from creating a new Singleton
25 ~Singleton(){} // Prevent clients from deleting a Singleton
26 Singleton(const Singleton&); // Prevent clients from copying a Singleton
27 Singleton& operator=(const Singleton&);
28 private:
29 static Mutex _mutex;
30 static Singleton volatile *_instance; // The one and only instance
//为了防止编译器优化加入volatile这个修饰关键字
31 };
32
33 #endif/*SINGLETON_H*/
singleton.cpp
1 #include "singleton.h"
2
3 Mutex Singleton::_mutex;
4 Singleton* volatile Singleton::_instance = 0; //为了防止编译器优化加入volatile这个修饰关键字
- Singleton [Double-Checked Locling] 双重加琐优化
- Singleton优化(Double-Checked Locking Pattern)
- Singleton 单件模式及其变体 Double-Checked Locking 双重检查锁模式
- Singleton 单件模式及其变体 Double-Checked Locking 双重检查锁模式
- Double-checked locking and the Singleton pattern
- Double-checked locking and the Singleton pattern
- Double-checked locking and the Singleton pattern
- Singleton 模式和Double-Checked Locking
- Double-checked locking and the Singleton pattern
- Double-checked locking and the Singleton pattern
- (设计模式)Singleton和Double-Checked Locking模式
- [转贴] Double-checked locking and the Singleton pattern
- Singleton pattern and problem with double checked locking
- 单例解析-Double-checked locking and the Singleton pattern
- Design Patterns I - Singleton, Double-Checked locking, Lazy Initialization
- 单例(Singleton) 双重锁定(Double-Check Locking)
- 双重检查锁定的漏洞的分析 The "Double-Checked Locking is Broken" Declaration
- 双重检查锁定失败可能性——参照《The "Double-Checked Locking is Broken" Declaration》
- 服装行业绣印花外发加工的跟踪方法
- Ehcache 1.5.0 User Guide - Introduction (介绍)(2)
- 传Sun CEO麦克尼利将离职 接班人尚无着落
- Linux计划任务读取mysq并发送邮件
- PHP的COOKIE设置为浏览器进程
- Singleton [Double-Checked Locling] 双重加琐优化
- Cookie及其使用(一)
- 哪有php 5电子书清晰版的
- PHP如何读取COOKIES
- IT 项目经理的职业生涯
- Cookie和会话状态的工作原理及Cookie欺骗
- Ehcache 1.5.0 User Guide - Getting Started(开始)
- Cookie工作原理
- 我有2台电脑想同时上网,怎么设置路由器啊