本文介紹C++單例模式的集中實(shí)現(xiàn)方式仅政,以及利弊
局部靜態(tài)變量方式
//通過(guò)靜態(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;
}
};
上述代碼通過(guò)局部靜態(tài)成員single實(shí)現(xiàn)單例類,原理就是函數(shù)的局部靜態(tài)變量生命周期隨著進(jìn)程結(jié)束而結(jié)束钦讳。上述代碼通過(guò)懶漢式的方式實(shí)現(xiàn)纳胧。
調(diào)用如下
void test_single2()
{
//多線程情況下可能存在問(wèn)題
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)成員變量指針?lè)绞?/h2>
可以定義一個(gè)類的靜態(tài)成員變量籍凝,用來(lá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è)好處是我們可以通過(guò)餓漢式的方式避免線程安全問(wèn)題
//餓漢式初始化
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
可見(jiàn)無(wú)論單線程還是多線程模式下周瞎,通過(guò)靜態(tài)成員變量的指針實(shí)現(xiàn)的單例類都是唯一的。餓漢式是在程序啟動(dòng)時(shí)就進(jìn)行單例的初始化饵蒂,這種方式也可以通過(guò)懶漢式調(diào)用声诸,無(wú)論餓漢式還是懶漢式都存在一個(gè)問(wèn)題,就是什么時(shí)候釋放內(nèi)存退盯?多線程情況下彼乌,釋放內(nèi)存就很難了,還有二次釋放內(nèi)存的風(fēng)險(xiǎn)渊迁。
我們定義一個(gè)單例類并用懶漢式方式調(diào)用
//懶漢式指針
//即使創(chuàng)建指針類型也存在問(wèn)題
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)存空間還沒(méi)回收,這是個(gè)問(wèn)題琉朽,另外如果多線程情況下多次delete也會(huì)造成崩潰毒租。
智能指針?lè)绞?/h2>
可以利用智能指針自動(dòng)回收內(nèi)存的機(jī)制設(shè)計(jì)單例類
//利用智能指針解決釋放問(wèn)題
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<SingleAuto>類型的變量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ù)
// 智能指針?lè)绞?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
智能指針?lè)绞讲淮嬖趦?nèi)存泄漏螟够,但是有一個(gè)隱患就是單例類的析構(gòu)函數(shù)時(shí)public的灾梦,如果被人手動(dòng)調(diào)用會(huì)存在崩潰問(wèn)題峡钓,比如將上邊test_singleauto中的注釋打開(kāi),程序會(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;
//定義友元類捧灰,通過(guò)友元類調(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要寫(xiě)在SingleAutoSafe上邊淆九,并且SafeDeletor要聲明為SingleAutoSafe類的友元類统锤,這樣就可以訪問(wèn)SingleAutoSafe的析構(gòu)函數(shù)了。
我們?cè)跇?gòu)造single時(shí)制定了SafeDeletor(),single在回收時(shí)炭庙,會(huì)調(diào)用SingleAutoSafe的仿函數(shù)饲窿,從而完成內(nèi)存的銷毀。
并且SingleAutoSafe的析構(gòu)函數(shù)為私有的無(wú)法被外界手動(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í)無(wú)法訪問(wèn)析構(gòu)函數(shù)逾雄,非常安全
// delete sp1.get();
}
int main(){
test_singleautosafe();
}
程序輸出如下
sp1 is 0x1264f30
sp2 is 0x1264f30
通過(guò)輔助類調(diào)用單例類的析構(gòu)函數(shù)保證了內(nèi)存釋放的安全性和唯一性。這種方式時(shí)生產(chǎn)中常用的腻脏。如果將test_singleautosafe函數(shù)的注釋打開(kāi)鸦泳,手動(dòng)delete sp1.get()編譯階段就會(huì)報(bào)錯(cuò),達(dá)到了代碼安全的目的永品。因?yàn)槲鰳?gòu)被設(shè)置為私有函數(shù)了做鹰。
通用的單例模板類
我們可以通過(guò)聲明單例的模板類,然后繼承這個(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)用.
//通過(guò)繼承方式實(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é)
本文介紹了一些面試常見(jiàn)問(wèn)題
源碼鏈接
https://gitee.com/secondtonone1/cpplearn
想系統(tǒng)學(xué)習(xí)更多C++知識(shí),可點(diǎn)擊下方鏈接。
C++基礎(chǔ)