C++單例模式的懶漢模式和餓漢模式詳解
懶漢模式
懶漢模式在第一次用到類(lèi)實(shí)例的時(shí)候才會(huì)去實(shí)例化,就是不到調(diào)用getInstance函數(shù)時(shí),這個(gè)類(lèi)的對(duì)象是一直不存在的。懶漢本身是線程不安全的。
#include <iostream>
using namespace std;
class Singelton{
private:
Singelton(){
m_count ++;
printf("Singelton begin\n");
Sleep(1000);// 加sleep為了放大效果
printf("Singelton end\n");
}
static Singelton *single;//定義一個(gè)唯一指向?qū)嵗闹羔槪⑶沂撬接械?
public:
static Singelton *GetSingelton();//定義一個(gè)公有函數(shù),可以獲取這個(gè)唯一實(shí)例
static void print();
static int m_count;
};
//將唯一指向?qū)嵗闹羔槼跏蓟癁閚ullptr
Singelton *Singelton::single = nullptr;
int Singelton::m_count = 0;
Singelton *Singelton::GetSingelton(){
if(single == nullptr){//判斷是不是第一次使用
single = new Singelton;
}
return single;
}
void Singelton::print(){
cout<<m_count<<endl;
}
int main()
{
singleton* a1 = singleton::GetInstance();
cout << a1 << endl;
a1->print();
singleton* a2 = singleton::GetInstance();
cout << a2 << endl;
a2->print();
system("pause");
return 0;
}
懶漢模式的singleton類(lèi)有以下特點(diǎn):
1.他有一個(gè)指向唯一實(shí)例的靜態(tài)指針,并且是私有的。
2.它有一個(gè)公有的函數(shù),可以獲取這個(gè)唯一的實(shí)例,并且在需要的時(shí)候創(chuàng)建該實(shí)例。
3.它的構(gòu)造函數(shù)是私有的,這樣就不能從別處創(chuàng)建該類(lèi)的實(shí)例。
餓漢模式
餓漢模式在單例類(lèi)定義的時(shí)候(即在main函數(shù)之前)就進(jìn)行實(shí)例化。因?yàn)閙ain函數(shù)執(zhí)行之前,全局作用域的類(lèi)成員靜態(tài)變量m_Instance已經(jīng)初始化,故沒(méi)有多線程的問(wèn)題。
#include <iostream>
#include <process.h>
#include <windows.h>
using namespace std;
class Singelton{
private:
Singelton(){
m_count ++;
printf("Singelton begin\n");
Sleep(1000); // 加sleep為了放大效果
printf("Singelton end\n");
}
static Singelton *single;//定義一個(gè)唯一指向?qū)嵗闹羔槪⑶沂撬接械?
public:
static Singelton *GetSingelton();//定義一個(gè)公有函數(shù),可以獲取這個(gè)唯一實(shí)例
static void print();
static int m_count;
};
// 餓漢模式的關(guān)鍵:定義即實(shí)例化
Singelton *Singelton::single = new Singelton;
int Singelton::m_count = 0;
Singelton *Singelton::GetSingelton(){
// 不再需要進(jìn)行實(shí)例化
//if(single == nullptr){
// single = new Singelton;
//}
return single;
}
void Singelton::print(){
cout<<m_count<<endl;
}
int main()
{
cout << "we get the instance" << endl;
singleton* a1 = singleton::getinstance();
singleton* a2 = singleton::getinstance();
singleton* a3 = singleton::getinstance();
cout << "we destroy the instance" << endl;
system("pause");
return 0;
}
線程安全的懶漢模式
在多線程環(huán)境下,懶漢模式的上述實(shí)現(xiàn)方式是不安全的,原因在于在判斷instance是否為空時(shí),可能存在多個(gè)線程同時(shí)進(jìn)入if中,此時(shí)可能會(huì)實(shí)例化多個(gè)對(duì)象。于是出現(xiàn)了二重鎖的懶漢模式,實(shí)現(xiàn)代碼如下:
#include<iostream>
#include<mutex>
using namespace std;
/*單例模式:構(gòu)造函數(shù)私有化,對(duì)外提供一個(gè)接口*/
//線程安全的單例模式
class lhsingleClass {
public:
static lhsingleClass* getinstance()
{//雙重鎖模式
if (instance == nullptr)
{//先判斷是否為空,如果為空則進(jìn)入,不為空說(shuō)明已經(jīng)存在實(shí)例,直接返回
//進(jìn)入后加鎖
i_mutex.lock();
if (instance == nullptr)
{//再判斷一次,確保不會(huì)因?yàn)榧渔i期間多個(gè)線程同時(shí)進(jìn)入
instance = new lhsingleClass();
}
i_mutex.unlock();//解鎖
}
return instance;
}
private:
static lhsingleClass* instance;
static mutex i_mutex;//鎖
lhsingleClass(){}
};
lhsingleClass* lhsingleClass::instance=nullptr;
mutex lhsingleClass::i_mutex;//類(lèi)外初始化
int main()
{
lhsingleClass* lhsinglep5 = lhsingleClass::getinstance();
lhsingleClass* lhsinglep6 = lhsingleClass::getinstance();
cout << lhsinglep5 << endl;
cout << lhsinglep6 << endl;
system("pause");
return 0;
}
此代碼共進(jìn)行了兩次判斷:
- 先判斷是否為空,如果為空則進(jìn)入,不為空說(shuō)明已經(jīng)存在實(shí)例,直接返回。
- 再判斷一次,確保不會(huì)因?yàn)榧渔i期間多個(gè)線程同時(shí)進(jìn)入。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式代碼示例
數(shù)組元素和數(shù)組名都可以作為函數(shù)的參數(shù)以實(shí)現(xiàn)函數(shù)間數(shù)據(jù)的傳遞和共享,下面這篇文章主要給大家介紹了關(guān)于C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
C++利用容器查找重復(fù)列功能實(shí)現(xiàn)
本文將詳細(xì)介紹c++容器簡(jiǎn)介,c++容器的比較 與操作實(shí)例,需要了解更多的朋友可以參考下2012-11-11
Vscode中接入Deepseek的實(shí)現(xiàn)
本文主要介紹了Vscode中接入Deepseek的實(shí)現(xiàn),包括登錄Deepseek官網(wǎng)、申請(qǐng)APIKEY、安裝和配置VSCode插件,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
C++報(bào)錯(cuò):Segmentation Fault的解決方案
段錯(cuò)誤(Segmentation Fault)是 C++ 編程中常見(jiàn)且令人頭疼的錯(cuò)誤之一,段錯(cuò)誤通常發(fā)生在程序試圖訪問(wèn)未被允許的內(nèi)存區(qū)域時(shí),導(dǎo)致程序崩潰,本文將深入探討段錯(cuò)誤的產(chǎn)生原因、檢測(cè)方法及其預(yù)防和解決方案,需要的朋友可以參考下2024-07-07
C++實(shí)現(xiàn)stack與queue數(shù)據(jù)結(jié)構(gòu)的模擬
stack是一種容器適配器,專(zhuān)門(mén)用在具有后進(jìn)先出操作的上下文環(huán)境中,其刪除只能從容器的一端進(jìn)行 元素的插入與提取操作;隊(duì)列是一種容器適配器,專(zhuān)門(mén)用于在FIFO上下文(先進(jìn)先出)中操作,其中從容器一端插入元素,另一端提取元素2023-04-04
C語(yǔ)言常見(jiàn)排序算法之交換排序(冒泡排序,快速排序)
這篇文章主要介紹了C語(yǔ)言常見(jiàn)排序算法之交換排序(冒泡排序,快速排序),冒泡排序即Bubble?Sort,類(lèi)似于水中冒泡,較大的數(shù)沉下去,較小的數(shù)慢慢冒起來(lái),假設(shè)從小到大,即為較大的數(shù)慢慢往后排,較小的數(shù)慢慢往前排2022-07-07

