1. 程式人生 > 資訊 >微信 Windows 版 3.6.0 正式版釋出:支援查詢微訊號並新增朋友

微信 Windows 版 3.6.0 正式版釋出:支援查詢微訊號並新增朋友

本文介紹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型別的變數single。因為single是靜態成員變數,所以會在程序結束時被回收。智慧指標被回收時會呼叫內建指標型別的解構函式,從而完成記憶體的回收。
在主函式呼叫如下測試函式

// 智慧指標方式
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++基礎