#include <iostream>
using namespace std;
class Mutex {
public:
void lock() {
cout << "Mutex::lock " << endl;
}
void unlock() {
cout << "Mutex::unlock " << endl;
}
};
class Cursor {
private:
Cursor() {}
Cursor(const Cursor& c);
Cursor& operator=(const Cursor& c);
static Cursor* sInstance;
static Mutex mutex;
public:
static Cursor& getInstance() {
//-------------------------
mutex.lock();
if(sInstance==0)
sInstance=new Cursor;
mutex.unlock();
//-------------------------
return *sInstance;
}
};
Cursor* Cursor::sInstance;
Mutex Cursor::mutex;
int main(void) {
Cursor& c1 = Cursor::getInstance();
Cursor& c2 = Cursor::getInstance();
cout << "&c1 = " << &c1 << endl;
cout << "&c2 = " << &c2 << endl;
return 0;
}
'Developer > Design Patterns' 카테고리의 다른 글
템플릿 상속을 이용한 싱글톤 패턴 (0) | 2014.05.17 |
---|---|
싱글톤 패턴 & RAII (0) | 2014.05.17 |
메크로를 이용한 SingleTon 자동생성 (1) | 2014.05.17 |
GoF's Singleton (객체를 힙에 생성) (0) | 2014.05.17 |
Mayer's Singleton (객체를 정적변수로 선언) (0) | 2014.05.17 |