C++單例模式的幾種實(shí)現(xiàn)方法詳解
局部靜態(tài)變量方式
//通過靜態(tài)成員變量實(shí)現(xiàn)單例 //懶漢式 class Single2 { private: Single2() { } Single2(const Single2 &) = delete; Single2 &operator=(const Single2 &) = delete; public: static Single2 &GetInst() { static Single2 single; return single; } };
上述代碼通過局部靜態(tài)成員single實(shí)現(xiàn)單例類,原理就是函數(shù)的局部靜態(tài)變量生命周期隨著進(jìn)程結(jié)束而結(jié)束。上述代碼通過懶漢式的方式實(shí)現(xiàn)。
調(diào)用如下
void test_single2() { //多線程情況下可能存在問題 cout << "s1 addr is " << &Single2::GetInst() << endl; cout << "s2 addr is " << &Single2::GetInst() << endl; }
程序輸出如下
sp1 is 0x1304b10
sp2 is 0x1304b10
確實(shí)生成了唯一實(shí)例,上述單例模式存在隱患,對(duì)于多線程方式生成的實(shí)例可能時(shí)多個(gè)。
靜態(tài)成員變量指針方式
可以定義一個(gè)類的靜態(tài)成員變量,用來控制實(shí)現(xiàn)單例
//餓漢式 class Single2Hungry { private: Single2Hungry() { } Single2Hungry(const Single2Hungry &) = delete; Single2Hungry &operator=(const Single2Hungry &) = delete; public: static Single2Hungry *GetInst() { if (single == nullptr) { single = new Single2Hungry(); } return single; } private: static Single2Hungry *single; };
這么做的一個(gè)好處是我們可以通過餓漢式的方式避免線程安全問題
//餓漢式初始化 Single2Hungry *Single2Hungry::single = Single2Hungry::GetInst(); void thread_func_s2(int i) { cout << "this is thread " << i << endl; cout << "inst is " << Single2Hungry::GetInst() << endl; } void test_single2hungry() { cout << "s1 addr is " << Single2Hungry::GetInst() << endl; cout << "s2 addr is " << Single2Hungry::GetInst() << endl; for (int i = 0; i < 3; i++) { thread tid(thread_func_s2, i); tid.join(); } } int main(){ test_single2hungry() }
程序輸出如下
s1 addr is 0x1e4b00
s2 addr is 0x1e4b00
this is thread 0
inst is 0x1e4b00
this is thread 1
inst is 0x1e4b00
this is thread 2
inst is 0x1e4b00
可見無論單線程還是多線程模式下,通過靜態(tài)成員變量的指針實(shí)現(xiàn)的單例類都是唯一的。餓漢式是在程序啟動(dòng)時(shí)就進(jìn)行單例的初始化,這種方式也可以通過懶漢式調(diào)用,無論餓漢式還是懶漢式都存在一個(gè)問題,就是什么時(shí)候釋放內(nèi)存?多線程情況下,釋放內(nèi)存就很難了,還有二次釋放內(nèi)存的風(fēng)險(xiǎn)。
我們定義一個(gè)單例類并用懶漢式方式調(diào)用
//懶漢式指針 //即使創(chuàng)建指針類型也存在問題 class SinglePointer { private: SinglePointer() { } SinglePointer(const SinglePointer &) = delete; SinglePointer &operator=(const SinglePointer &) = delete; public: static SinglePointer *GetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } single = new SinglePointer(); s_mutex.unlock(); return single; } private: static SinglePointer *single; static mutex s_mutex; };
在cpp文件里初始化靜態(tài)成員,并定義一個(gè)測(cè)試函數(shù)
//懶漢式 //在類的cpp文件定義static變量 SinglePointer *SinglePointer::single = nullptr; std::mutex SinglePointer::s_mutex; void thread_func_lazy(int i) { cout << "this is lazy thread " << i << endl; cout << "inst is " << SinglePointer::GetInst() << endl; } void test_singlelazy() { for (int i = 0; i < 3; i++) { thread tid(thread_func_lazy, i); tid.join(); } //何時(shí)釋放new的對(duì)象?造成內(nèi)存泄漏 } int main(){ test_singlelazy(); }
函數(shù)輸出如下
this is lazy thread 0 inst is 0xbc1700 this is lazy thread 1 inst is 0xbc1700 this is lazy thread 2 inst is 0xbc1700
此時(shí)生成的單例對(duì)象的內(nèi)存空間還沒回收,這是個(gè)問題,另外如果多線程情況下多次delete也會(huì)造成崩潰。
智能指針方式
可以利用智能指針自動(dòng)回收內(nèi)存的機(jī)制設(shè)計(jì)單例類
//利用智能指針解決釋放問題 class SingleAuto { private: SingleAuto() { } SingleAuto(const SingleAuto &) = delete; SingleAuto &operator=(const SingleAuto &) = delete; public: ~SingleAuto() { cout << "single auto delete success " << endl; } static std::shared_ptr<SingleAuto> GetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } single = std::shared_ptr<SingleAuto>(new SingleAuto); s_mutex.unlock(); return single; } private: static std::shared_ptr<SingleAuto> single; static mutex s_mutex; };
SingleAuto的GetInst返回std::shared_ptr類型的變量single。因?yàn)閟ingle是靜態(tài)成員變量,所以會(huì)在進(jìn)程結(jié)束時(shí)被回收。智能指針被回收時(shí)會(huì)調(diào)用內(nèi)置指針類型的析構(gòu)函數(shù),從而完成內(nèi)存的回收。
在主函數(shù)調(diào)用如下測(cè)試函數(shù)
// 智能指針方式 std::shared_ptr<SingleAuto> SingleAuto::single = nullptr; mutex SingleAuto::s_mutex; void test_singleauto() { auto sp1 = SingleAuto::GetInst(); auto sp2 = SingleAuto::GetInst(); cout << "sp1 is " << sp1 << endl; cout << "sp2 is " << sp2 << endl; //此時(shí)存在隱患,可以手動(dòng)刪除裸指針,造成崩潰 // delete sp1.get(); } int main(){ test_singleauto(); }
程序輸出如下
sp1 is 0x1174f30
sp2 is 0x1174f30
智能指針方式不存在內(nèi)存泄漏,但是有一個(gè)隱患就是單例類的析構(gòu)函數(shù)時(shí)public的,如果被人手動(dòng)調(diào)用會(huì)存在崩潰問題,比如將上邊test_singleauto中的注釋打開,程序會(huì)崩潰。
輔助類智能指針單例模式
智能指針在構(gòu)造的時(shí)候可以指定刪除器,所以可以傳遞一個(gè)輔助類或者輔助函數(shù)幫助智能指針回收內(nèi)存時(shí)調(diào)用我們指定的析構(gòu)函數(shù)。
// safe deletor //防止外界delete //聲明輔助類 //該類定義仿函數(shù)調(diào)用SingleAutoSafe析構(gòu)函數(shù) //不可以提前聲明SafeDeletor,編譯時(shí)會(huì)提示incomplete type // class SafeDeletor; //所以要提前定義輔助類 class SingleAutoSafe; class SafeDeletor { public: void operator()(SingleAutoSafe *sf) { cout << "this is safe deleter operator()" << endl; delete sf; } }; class SingleAutoSafe { private: SingleAutoSafe() {} ~SingleAutoSafe() { cout << "this is single auto safe deletor" << endl; } SingleAutoSafe(const SingleAutoSafe &) = delete; SingleAutoSafe &operator=(const SingleAutoSafe &) = delete; //定義友元類,通過友元類調(diào)用該類析構(gòu)函數(shù) friend class SafeDeletor; public: static std::shared_ptr<SingleAutoSafe> GetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } //額外指定刪除器 single = std::shared_ptr<SingleAutoSafe>(new SingleAutoSafe, SafeDeletor()); //也可以指定刪除函數(shù) // single = std::shared_ptr<SingleAutoSafe>(new SingleAutoSafe, SafeDelFunc); s_mutex.unlock(); return single; } private: static std::shared_ptr<SingleAutoSafe> single; static mutex s_mutex; };
SafeDeletor要寫在SingleAutoSafe上邊,并且SafeDeletor要聲明為SingleAutoSafe類的友元類,這樣就可以訪問SingleAutoSafe的析構(gòu)函數(shù)了。
我們?cè)跇?gòu)造single時(shí)制定了SafeDeletor(),single在回收時(shí),會(huì)調(diào)用SingleAutoSafe的仿函數(shù),從而完成內(nèi)存的銷毀。
并且SingleAutoSafe的析構(gòu)函數(shù)為私有的無法被外界手動(dòng)調(diào)用了。
//智能指針初始化為nullptr std::shared_ptr<SingleAutoSafe> SingleAutoSafe::single = nullptr; mutex SingleAutoSafe::s_mutex; void test_singleautosafe() { auto sp1 = SingleAutoSafe::GetInst(); auto sp2 = SingleAutoSafe::GetInst(); cout << "sp1 is " << sp1 << endl; cout << "sp2 is " << sp2 << endl; //此時(shí)無法訪問析構(gòu)函數(shù),非常安全 // delete sp1.get(); } int main(){ test_singleautosafe(); }
程序輸出如下
sp1 is 0x1264f30 sp2 is 0x1264f30
通過輔助類調(diào)用單例類的析構(gòu)函數(shù)保證了內(nèi)存釋放的安全性和唯一性。這種方式時(shí)生產(chǎn)中常用的。如果將test_singleautosafe函數(shù)的注釋打開,手動(dòng)delete sp1.get()編譯階段就會(huì)報(bào)錯(cuò),達(dá)到了代碼安全的目的。因?yàn)槲鰳?gòu)被設(shè)置為私有函數(shù)了。
通用的單例模板類
我們可以通過聲明單例的模板類,然后繼承這個(gè)單例模板類的所有類就是單例類了。達(dá)到泛型編程提高效率的目的。
template <typename T> class Single_T { protected: Single_T() = default; Single_T(const Single_T<T> &st) = delete; Single_T &operator=(const Single_T<T> &st) = delete; ~Single_T() { cout << "this is auto safe template destruct" << endl; } public: static std::shared_ptr<T> GetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } //額外指定刪除器 single = std::shared_ptr<T>(new T, SafeDeletor_T<T>()); //也可以指定刪除函數(shù) // single = std::shared_ptr<SingleAutoSafe>(new SingleAutoSafe, SafeDelFunc); s_mutex.unlock(); return single; } private: static std::shared_ptr<T> single; static mutex s_mutex; }; //模板類的static成員要放在h文件里初始化 template <typename T> std::shared_ptr<T> Single_T<T>::single = nullptr; template <typename T> mutex Single_T<T>::s_mutex;
我們定義一個(gè)網(wǎng)絡(luò)的單例類,繼承上述模板類即可,并將構(gòu)造和析構(gòu)設(shè)置為私有,同時(shí)設(shè)置友元保證自己的析構(gòu)和構(gòu)造可以被友元類調(diào)用.
//通過繼承方式實(shí)現(xiàn)網(wǎng)絡(luò)模塊單例 class SingleNet : public Single_T<SingleNet> { private: SingleNet() = default; SingleNet(const SingleNet &) = delete; SingleNet &operator=(const SingleNet &) = delete; ~SingleNet() = default; friend class SafeDeletor_T<SingleNet>; friend class Single_T<SingleNet>; };
在主函數(shù)中調(diào)用如下
void test_singlenet() { auto sp1 = SingleNet::GetInst(); auto sp2 = SingleNet::GetInst(); cout << "sp1 is " << sp1 << endl; cout << "sp2 is " << sp2 << endl; }
程序輸出如下
sp1 is 0x1164f30
sp2 is 0x1164f30
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Qt實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C++實(shí)現(xiàn)LeetCode(87.攪亂字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(87.攪亂字符串),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07mfc入門教程之實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器
這篇文章主要介紹了mfc入門教程,手把手教你如何開發(fā)一個(gè)簡(jiǎn)單的計(jì)算器,需要的朋友可以參考下2019-04-04如何在c++中實(shí)現(xiàn)字符串分割函數(shù)split詳解
這篇文章主要給大家介紹了關(guān)于如何在c++中實(shí)現(xiàn)字符串分割函數(shù)split的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11