本文介紹C++單例模式的集中實現方式,以及利弊
局部靜態變量方式
//通過靜態成員變量實現單例
//懶漢式
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_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
在主函數調用如下測試函數
// 智能指針方式
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;
//此時存在隱患,可以手動刪除裸指針,造成崩潰
// 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_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());
//也可以指定刪除函數
// 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的析構函數了。
我們在構造single時制定了SafeDeletor(),single在回收時,會調用SingleAutoSafe的仿函數,從而完成內存的銷毀。
並且SingleAutoSafe的析構函數為私有的無法被外界手動調用了。
//智能指針初始化為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;
//此時無法訪問析構函數,非常安全
// delete sp1.get();
}
int main(){
test_singleautosafe();
}
程序輸出如下
sp1 is 0x1264f30
sp2 is 0x1264f30
通過輔助類調用單例類的析構函數保證了內存釋放的安全性和唯一性。這種方式時生產中常用的。如果將test_singleautosafe函數的注釋打開,手動delete sp1.get()編譯階段就會報錯,達到了代碼安全的目的。因為析構被設置為私有函數了。
通用的單例模板類
我們可以通過聲明單例的模板類,然后繼承這個單例模板類的所有類就是單例類了。達到泛型編程提高效率的目的。
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>());
//也可以指定刪除函數
// 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;
我們定義一個網絡的單例類,繼承上述模板類即可,並將構造和析構設置為私有,同時設置友元保證自己的析構和構造可以被友元類調用.
//通過繼承方式實現網絡模塊單例
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>;
};
在主函數中調用如下
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://gitee.com/secondtonone1/cpplearn
想系統學習更多C++知識,可點擊下方鏈接。
C++基礎