C++單例模式的幾種實(shí)現(xiàn)方法詳解_第1頁
C++單例模式的幾種實(shí)現(xiàn)方法詳解_第2頁
C++單例模式的幾種實(shí)現(xiàn)方法詳解_第3頁
C++單例模式的幾種實(shí)現(xiàn)方法詳解_第4頁
C++單例模式的幾種實(shí)現(xiàn)方法詳解_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第C++單例模式的幾種實(shí)現(xiàn)方法詳解目錄局部靜態(tài)變量方式靜態(tài)成員變量指針方式智能指針方式輔助類智能指針單例模式通用的單例模板類總結(jié)

局部靜態(tài)變量方式

//通過靜態(tài)成員變量實(shí)現(xiàn)單例

//懶漢式

classSingle2

private:

Single2()

Single2(constSingle2)=delete;

Single2operator=(constSingle2)=delete;

public:

staticSingle2GetInst()

staticSingle2single;

returnsingle;

上述代碼通過局部靜態(tài)成員single實(shí)現(xiàn)單例類,原理就是函數(shù)的局部靜態(tài)變量生命周期隨著進(jìn)程結(jié)束而結(jié)束。上述代碼通過懶漢式的方式實(shí)現(xiàn)。

調(diào)用如下

voidtest_single2()

//多線程情況下可能存在問題

cout"s1addris"Single2::GetInst()endl;

cout"s2addris"Single2::GetInst()endl;

程序輸出如下

sp1is0x1304b10

sp2is0x1304b10

確實(shí)生成了唯一實(shí)例,上述單例模式存在隱患,對于多線程方式生成的實(shí)例可能時多個。

靜態(tài)成員變量指針方式

可以定義一個類的靜態(tài)成員變量,用來控制實(shí)現(xiàn)單例

//餓漢式

classSingle2Hungry

private:

Single2Hungry()

Single2Hungry(constSingle2Hungry)=delete;

Single2Hungryoperator=(constSingle2Hungry)=delete;

public:

staticSingle2Hungry*GetInst()

if(single==nullptr)

single=newSingle2Hungry();

returnsingle;

private:

staticSingle2Hungry*single;

這么做的一個好處是我們可以通過餓漢式的方式避免線程安全問題

//餓漢式初始化

Single2Hungry*Single2Hungry::single=Single2Hungry::GetInst();

voidthread_func_s2(inti)

cout"thisisthread"iendl;

cout"instis"Single2Hungry::GetInst()endl;

voidtest_single2hungry()

cout"s1addris"Single2Hungry::GetInst()endl;

cout"s2addris"Single2Hungry::GetInst()endl;

for(inti=0;ii++)

threadtid(thread_func_s2,i);

tid.join();

intmain(){

test_single2hungry()

程序輸出如下

s1addris0x1e4b00

s2addris0x1e4b00

thisisthread0

instis0x1e4b00

thisisthread1

instis0x1e4b00

thisisthread2

instis0x1e4b00

可見無論單線程還是多線程模式下,通過靜態(tài)成員變量的指針實(shí)現(xiàn)的單例類都是唯一的。餓漢式是在程序啟動時就進(jìn)行單例的初始化,這種方式也可以通過懶漢式調(diào)用,無論餓漢式還是懶漢式都存在一個問題,就是什么時候釋放內(nèi)存?多線程情況下,釋放內(nèi)存就很難了,還有二次釋放內(nèi)存的風(fēng)險。

我們定義一個單例類并用懶漢式方式調(diào)用

//懶漢式指針

//即使創(chuàng)建指針類型也存在問題

classSinglePointer

private:

SinglePointer()

SinglePointer(constSinglePointer)=delete;

SinglePointeroperator=(constSinglePointer)=delete;

public:

staticSinglePointer*GetInst()

if(single!=nullptr)

returnsingle;

s_mutex.lock();

if(single!=nullptr)

s_mutex.unlock();

returnsingle;

single=newSinglePointer();

s_mutex.unlock();

returnsingle;

private:

staticSinglePointer*single;

staticmutexs_mutex;

在cpp文件里初始化靜態(tài)成員,并定義一個測試函數(shù)

//懶漢式

//在類的cpp文件定義static變量

SinglePointer*SinglePointer::single=nullptr;

std::mutexSinglePointer::s_mutex;

voidthread_func_lazy(inti)

cout"thisislazythread"iendl;

cout"instis"SinglePointer::GetInst()endl;

voidtest_singlelazy()

for(inti=0;ii++)

threadtid(thread_func_lazy,i);

tid.join();

//何時釋放new的對象?造成內(nèi)存泄漏

intmain(){

test_singlelazy();

函數(shù)輸出如下

thisislazythread0

instis0xbc1700

thisislazythread1

instis0xbc1700

thisislazythread2

instis0xbc1700

此時生成的單例對象的內(nèi)存空間還沒回收,這是個問題,另外如果多線程情況下多次delete也會造成崩潰。

智能指針方式

可以利用智能指針自動回收內(nèi)存的機(jī)制設(shè)計單例類

//利用智能指針解決釋放問題

classSingleAuto

private:

SingleAuto()

SingleAuto(constSingleAuto)=delete;

SingleAutooperator=(constSingleAuto)=delete;

public:

~SingleAuto()

cout"singleautodeletesuccess"endl;

staticstd::shared_ptrSingleAutoGetInst()

if(single!=nullptr)

returnsingle;

s_mutex.lock();

if(single!=nullptr)

s_mutex.unlock();

returnsingle;

single=std::shared_ptrSingleAuto(newSingleAuto);

s_mutex.unlock();

returnsingle;

private:

staticstd::shared_ptrSingleAutosingle;

staticmutexs_mutex;

SingleAuto的GetInst返回std::shared_ptr類型的變量single。因?yàn)閟ingle是靜態(tài)成員變量,所以會在進(jìn)程結(jié)束時被回收。智能指針被回收時會調(diào)用內(nèi)置指針類型的析構(gòu)函數(shù),從而完成內(nèi)存的回收。

在主函數(shù)調(diào)用如下測試函數(shù)

//智能指針方式

std::shared_ptrSingleAutoSingleAuto::single=nullptr;

mutexSingleAuto::s_mutex;

voidtest_singleauto()

autosp1=SingleAuto::GetInst();

autosp2=SingleAuto::GetInst();

cout"sp1is"sp1endl;

cout"sp2is"sp2endl;

//此時存在隱患,可以手動刪除裸指針,造成崩潰

//deletesp1.get();

intmain(){

test_singleauto();

程序輸出如下

sp1is0x1174f30

sp2is0x1174f30

智能指針方式不存在內(nèi)存泄漏,但是有一個隱患就是單例類的析構(gòu)函數(shù)時public的,如果被人手動調(diào)用會存在崩潰問題,比如將上邊test_singleauto中的注釋打開,程序會崩潰。

輔助類智能指針單例模式

智能指針在構(gòu)造的時候可以指定刪除器,所以可以傳遞一個輔助類或者輔助函數(shù)幫助智能指針回收內(nèi)存時調(diào)用我們指定的析構(gòu)函數(shù)。

//safedeletor

//防止外界delete

//聲明輔助類

//該類定義仿函數(shù)調(diào)用SingleAutoSafe析構(gòu)函數(shù)

//不可以提前聲明SafeDeletor,編譯時會提示incompletetype

//classSafeDeletor;

//所以要提前定義輔助類

classSingleAutoSafe;

classSafeDeletor

public:

voidoperator()(SingleAutoSafe*sf)

cout"thisissafedeleteroperator()"endl;

deletesf;

classSingleAutoSafe

private:

SingleAutoSafe(){}

~SingleAutoSafe()

cout"thisissingleautosafedeletor"endl;

SingleAutoSafe(constSingleAutoSafe)=delete;

SingleAutoSafeoperator=(constSingleAutoSafe)=delete;

//定義友元類,通過友元類調(diào)用該類析構(gòu)函數(shù)

friendclassSafeDeletor;

public:

staticstd::shared_ptrSingleAutoSafeGetInst()

if(single!=nullptr)

returnsingle;

s_mutex.lock();

if(single!=nullptr)

s_mutex.unlock();

returnsingle;

//額外指定刪除器

single=std::shared_ptrSingleAutoSafe(newSingleAutoSafe,SafeDeletor());

//也可以指定刪除函數(shù)

//single=std::shared_ptrSingleAutoSafe(newSingleAutoSafe,SafeDelFunc);

s_mutex.unlock();

returnsingle;

private:

staticstd::shared_ptrSingleAutoSafesingle;

staticmutexs_mutex;

SafeDeletor要寫在SingleAutoSafe上邊,并且SafeDeletor要聲明為SingleAutoSafe類的友元類,這樣就可以訪問SingleAutoSafe的析構(gòu)函數(shù)了。

我們在構(gòu)造single時制定了SafeDeletor(),single在回收時,會調(diào)用SingleAutoSafe的仿函數(shù),從而完成內(nèi)存的銷毀。

并且SingleAutoSafe的析構(gòu)函數(shù)為私有的無法被外界手動調(diào)用了。

//智能指針初始化為nullptr

std::shared_ptrSingleAutoSafeSingleAutoSafe::single=nullptr;

mutexSingleAutoSafe::s_mutex;

voidtest_singleautosafe()

autosp1=SingleAutoSafe::GetInst();

autosp2=SingleAutoSafe::GetInst();

cout"sp1is"sp1endl;

cout"sp2is"sp2endl;

//此時無法訪問析構(gòu)函數(shù),非常安全

//deletesp1.get();

intmain(){

test_singleautosafe();

程序輸出如下

sp1is0x1264f30

sp2is0x1264f30

通過輔助類調(diào)用單例類的析構(gòu)函數(shù)保證了內(nèi)存釋放的安全性和唯一性。這種方式時生產(chǎn)中常用的。如果將test_singleautosafe函數(shù)的注釋打開,手動deletesp1.get()編譯階段就會報錯,達(dá)到了代碼安全的目的。因?yàn)槲鰳?gòu)被設(shè)置為私有函數(shù)了。

通用的單例模板類

我們可以通過聲明單例的模板類,然后繼承這個單例模板類的所有類就是單例類了。達(dá)到泛型編程提高效率的目的。

templatetypenameT

classSingle_T

protected:

Single_T()=default;

Single_T(constSingle_TTst)=delete;

Single_Toperator=(constSingle_TTst)=delete;

~Single_T()

cout"thisisautosafetemplatedestruct"endl;

public:

staticstd::shared_ptrTGetInst()

if(single!=nullptr)

returnsingle;

s_mutex.lock();

if(single!=nullptr)

s_mutex.unlock();

returnsingle;

//額外指定刪除器

single=std::shared_ptrT(newT,SafeDeletor_TT

//也可以指定刪除函數(shù)

//single=std::shared_ptrSingleAutoSafe(newSingleAutoSafe,SafeDelFunc);

s_mutex.unlock();

returnsingle;

private:

staticstd::shared_ptrTsingle;

staticmutexs_mutex;

//模板類的static成

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論