網站首頁 編程語言 正文
局部靜態變量方式
//通過靜態成員變量實現單例 //懶漢式 class Single2 { private: Single2() { } Single2(const Single2 &) = delete; Single2 &operator=(const Single2 &) = delete; public: static Single2 &GetInst() { static Single2 single; return single; } };
上述代碼通過局部靜態成員single實現單例類,原理就是函數的局部靜態變量生命周期隨著進程結束而結束。上述代碼通過懶漢式的方式實現。
調用如下
void test_single2() { //多線程情況下可能存在問題 cout << "s1 addr is " << &Single2::GetInst() << endl; cout << "s2 addr is " << &Single2::GetInst() << endl; }
程序輸出如下
sp1 ?is ?0x1304b10
sp2 ?is ?0x1304b10
確實生成了唯一實例,上述單例模式存在隱患,對于多線程方式生成的實例可能時多個。
靜態成員變量指針方式
可以定義一個類的靜態成員變量,用來控制實現單例
//餓漢式 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; };
這么做的一個好處是我們可以通過餓漢式的方式避免線程安全問題
//餓漢式初始化 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
可見無論單線程還是多線程模式下,通過靜態成員變量的指針實現的單例類都是唯一的。餓漢式是在程序啟動時就進行單例的初始化,這種方式也可以通過懶漢式調用,無論餓漢式還是懶漢式都存在一個問題,就是什么時候釋放內存?多線程情況下,釋放內存就很難了,還有二次釋放內存的風險。
我們定義一個單例類并用懶漢式方式調用
//懶漢式指針 //即使創建指針類型也存在問題 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文件里初始化靜態成員,并定義一個測試函數
//懶漢式 //在類的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(); } //何時釋放new的對象?造成內存泄漏 } int main(){ test_singlelazy(); }
函數輸出如下
this is lazy thread 0 inst is 0xbc1700 this is lazy thread 1 inst is 0xbc1700 this is lazy thread 2 inst is 0xbc1700
此時生成的單例對象的內存空間還沒回收,這是個問題,另外如果多線程情況下多次delete也會造成崩潰。
智能指針方式
可以利用智能指針自動回收內存的機制設計單例類
//利用智能指針解決釋放問題 class SingleAuto { private: SingleAuto() { } SingleAuto(const SingleAuto &) = delete; SingleAuto &operator=(const SingleAuto &) = delete; public: ~SingleAuto() { cout << "single auto delete success " << endl; } static std::shared_ptrGetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } single = std::shared_ptr (new SingleAuto); s_mutex.unlock(); return single; } private: static std::shared_ptr single; static mutex s_mutex; };
SingleAuto的GetInst返回std::shared_ptr類型的變量single。因為single是靜態成員變量,所以會在進程結束時被回收。智能指針被回收時會調用內置指針類型的析構函數,從而完成內存的回收。
在主函數調用如下測試函數
// 智能指針方式 std::shared_ptrSingleAuto::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; //此時存在隱患,可以手動刪除裸指針,造成崩潰 // delete sp1.get(); } int main(){ test_singleauto(); }
程序輸出如下
sp1 ?is ?0x1174f30
sp2 ?is ?0x1174f30
智能指針方式不存在內存泄漏,但是有一個隱患就是單例類的析構函數時public的,如果被人手動調用會存在崩潰問題,比如將上邊test_singleauto中的注釋打開,程序會崩潰。
輔助類智能指針單例模式
智能指針在構造的時候可以指定刪除器,所以可以傳遞一個輔助類或者輔助函數幫助智能指針回收內存時調用我們指定的析構函數。
// safe deletor //防止外界delete //聲明輔助類 //該類定義仿函數調用SingleAutoSafe析構函數 //不可以提前聲明SafeDeletor,編譯時會提示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; //定義友元類,通過友元類調用該類析構函數 friend class SafeDeletor; public: static std::shared_ptrGetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } //額外指定刪除器 single = std::shared_ptr (new SingleAutoSafe, SafeDeletor()); //也可以指定刪除函數 // single = std::shared_ptr (new SingleAutoSafe, SafeDelFunc); s_mutex.unlock(); return single; } private: static std::shared_ptr single; static mutex s_mutex; };
SafeDeletor要寫在SingleAutoSafe上邊,并且SafeDeletor要聲明為SingleAutoSafe類的友元類,這樣就可以訪問SingleAutoSafe的析構函數了。
我們在構造single時制定了SafeDeletor(),single在回收時,會調用SingleAutoSafe的仿函數,從而完成內存的銷毀。
并且SingleAutoSafe的析構函數為私有的無法被外界手動調用了。
//智能指針初始化為nullptr std::shared_ptrSingleAutoSafe::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; //此時無法訪問析構函數,非常安全 // delete sp1.get(); } int main(){ test_singleautosafe(); }
程序輸出如下
sp1 is 0x1264f30 sp2 is 0x1264f30
通過輔助類調用單例類的析構函數保證了內存釋放的安全性和唯一性。這種方式時生產中常用的。如果將test_singleautosafe函數的注釋打開,手動delete sp1.get()編譯階段就會報錯,達到了代碼安全的目的。因為析構被設置為私有函數了。
通用的單例模板類
我們可以通過聲明單例的模板類,然后繼承這個單例模板類的所有類就是單例類了。達到泛型編程提高效率的目的。
templateclass Single_T { protected: Single_T() = default; Single_T(const Single_T &st) = delete; Single_T &operator=(const Single_T &st) = delete; ~Single_T() { cout << "this is auto safe template destruct" << endl; } public: static std::shared_ptr GetInst() { if (single != nullptr) { return single; } s_mutex.lock(); if (single != nullptr) { s_mutex.unlock(); return single; } //額外指定刪除器 single = std::shared_ptr (new T, SafeDeletor_T ()); //也可以指定刪除函數 // single = std::shared_ptr (new SingleAutoSafe, SafeDelFunc); s_mutex.unlock(); return single; } private: static std::shared_ptr single; static mutex s_mutex; }; //模板類的static成員要放在h文件里初始化 template std::shared_ptr Single_T ::single = nullptr; template mutex Single_T ::s_mutex;
我們定義一個網絡的單例類,繼承上述模板類即可,并將構造和析構設置為私有,同時設置友元保證自己的析構和構造可以被友元類調用.
//通過繼承方式實現網絡模塊單例 class SingleNet : public Single_T{ private: SingleNet() = default; SingleNet(const SingleNet &) = delete; SingleNet &operator=(const SingleNet &) = delete; ~SingleNet() = default; friend class SafeDeletor_T ; friend class Single_T ; };
在主函數中調用如下
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
總結
原文鏈接:https://blog.csdn.net/secondtonone1/article/details/123616175
- 上一篇:C++的拷貝構造函數你了解嗎_C 語言
- 下一篇:iOS實現無限滑動效果_IOS
相關推薦
- 2022-04-10 python?tkinter實現簡單計算器功能_python
- 2022-12-01 Apache?Doris?Join?優化原理詳解_Linux
- 2023-02-10 Golang中interface的基本用法詳解_Golang
- 2022-09-05 Hbase 之KeyValue結構詳解
- 2023-02-09 Python如何提取html中文本到txt_python
- 2022-02-15 多標簽界面:動態組件 & 異步組件
- 2022-09-15 python?Pandas庫read_excel()參數實例詳解_python
- 2023-01-20 C#實現自定義動畫鼠標的示例詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支