原文鏈接:http://www.lisongze.com/2018/07/26/sinleton-pattern/ or http://www.lisongze.cn/2018/07/26/sinleton-pattern/
1. 單例模式 (C++實現)
單例模式:保證一個類只有一個對象實例,並提供一個訪問該對象實例的全局訪問點。
單例模式有兩種實現方法:懶漢模式和餓漢模式。
1.1 懶漢模式
懶漢模式:故名思義,不到萬不得已就不會去實例化類,也就是說在第一次用到類實例時候纔會去實例化它。
在訪問量較小時,採用懶漢模式,這裏是以時間換空間。
線程安全的懶漢實現:
Singleton.cpp
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
class Singleton;
class Singleton {
private:
static Singleton *gInstance;
static pthread_mutex_t g_tMutex;
public:
static Singleton *getInstance()
{
if (NULL == gInstance)//雙重鎖定,提高多線程性能
{
pthread_mutex_lock(&g_tMutex);
if (NULL == gInstance)
gInstance = new Singleton;
pthread_mutex_unlock(&g_tMutex);
}
return gInstance;
}
void printInfo(){ cout<<"This is singleton"<<endl; }
private:
Singleton()
{
cout<<"Singleton()"<<endl;
}
};
/* 懶漢模式 */
Singleton *Singleton::gInstance;
pthread_mutex_t Singleton::g_tMutex = PTHREAD_MUTEX_INITIALIZER;
/* ------------------------------- for test --------------------------------------- */
void *start_routine_thread1(void *arg)
{
cout<<"this is thread 1 ..."<<endl;
Singleton *s = Singleton::getInstance();
s->printInfo();
return NULL;
}
void *start_routine_thread2(void *arg)
{
cout<<"this is thread 2 ..."<<endl;
Singleton *s = Singleton::getInstance();
s->printInfo();
return NULL;
}
int main()
{
Singleton *s = Singleton::getInstance();
s->printInfo();
Singleton *s2 = Singleton::getInstance();
s2->printInfo();
Singleton *s3 = Singleton::getInstance();
s3->printInfo();
/* 創建線程,在線程裏也去調用Singleton::getInstance */
pthread_t thread1ID;
pthread_t thread2ID;
pthread_create(&thread1ID, NULL, start_routine_thread1, NULL);
pthread_create(&thread2ID, NULL, start_routine_thread2, NULL);
sleep(3);
return 0;
}
測試:
$ g++ Singleton.cpp -pthread
$ ./a.out
Singleton()
This is singleton
This is singleton
This is singleton
this is thread 1 ...
This is singleton
this is thread 2 ...
This is singleton
1.2 餓漢模式
餓漢模式:餓了肯定要飢不擇食,所以在單例類定義的時候就進行實例化。
在訪問的線程比較多時,採用餓漢模式,可以實現更好的性能,這裏是以空間換時間。餓漢模式線程是安全的,因爲一開始已經對單例類進行的實例化。
餓漢模式的實現:
Singleton2.cpp
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
class Singleton;
class Singleton {
private:
static Singleton *gInstance;
public:
static Singleton *getInstance()
{
return gInstance;
}
void printInfo(){ cout<<"This is singleton"<<endl; }
private:
Singleton()
{
cout<<"Singleton()"<<endl;
}
};
/* 餓漢模式 */
Singleton *Singleton::gInstance = new Singleton;
/* ------------------------------- for test --------------------------------------- */
void *start_routine_thread1(void *arg)
{
cout<<"this is thread 1 ..."<<endl;
Singleton *s = Singleton::getInstance();
s->printInfo();
return NULL;
}
void *start_routine_thread2(void *arg)
{
cout<<"this is thread 2 ..."<<endl;
Singleton *s = Singleton::getInstance();
s->printInfo();
return NULL;
}
int main()
{
Singleton *s = Singleton::getInstance();
s->printInfo();
Singleton *s2 = Singleton::getInstance();
s2->printInfo();
Singleton *s3 = Singleton::getInstance();
s3->printInfo();
/* 創建線程,在線程裏也去調用Singleton::getInstance */
pthread_t thread1ID;
pthread_t thread2ID;
pthread_create(&thread1ID, NULL, start_routine_thread1, NULL);
pthread_create(&thread2ID, NULL, start_routine_thread2, NULL);
sleep(3);
return 0;
}
調試:
$ g++ Singleton2.cpp -pthread
$ ./a.out
Singleton()
This is singleton
This is singleton
This is singleton
this is thread 1 ...
this is thread 2 ...
This is singleton
This is singleton