亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用設計模式中的單例模式來實現(xiàn)C++的boost庫

 更新時間:2024年07月01日 16:36:30   作者:bang  
這篇文章主要介紹了使用設計模式中的單例模式來實現(xiàn)C++的boost庫的方法,其中作者對線程安全格外強調(diào),需要的朋友可以參考下

線程安全的單例模式
一、懶漢模式
:即第一次調(diào)用該類實例的時候才產(chǎn)生一個新的該類實例,并在以后僅返回此實例。

需要用鎖,來保證其線程安全性:原因:多個線程可能進入判斷是否已經(jīng)存在實例的if語句,從而non thread safety。

使用double-check來保證thread safety。但是如果處理大量數(shù)據(jù)時,該鎖才成為嚴重的性能瓶頸。

1、靜態(tài)成員實例的懶漢模式:

class Singleton
{
private:
  static Singleton* m_instance;
  Singleton(){}
public:
  static Singleton* getInstance();
};
 
Singleton* Singleton::getInstance()
{
  if(NULL == m_instance)
  {
    Lock();
//借用其它類來實現(xiàn),如boost
    if(NULL == m_instance)
    {
      m_instance = new Singleton;
    }
    UnLock();
  }
  return m_instance;
}

2、內(nèi)部靜態(tài)實例的懶漢模式

這里需要注意的是,C++0X以后,要求編譯器保證內(nèi)部靜態(tài)變量的線程安全性,可以不加鎖。但C++ 0X以前,仍需要加鎖。

class SingletonInside
{
private:
  SingletonInside(){}
public:
  static SingletonInside* getInstance()
  {
    Lock(); 
// not needed after C++0x
    static SingletonInside instance;
    UnLock(); 
// not needed after C++0x
    return instance; 
  }
};

二、餓漢模式:即無論是否調(diào)用該類的實例,在程序開始時就會產(chǎn)生一個該類的實例,并在以后僅返回此實例。

由靜態(tài)初始化實例保證其線程安全性,WHY?因為靜態(tài)實例初始化在程序開始時進入主函數(shù)之前就由主線程以單線程方式完成了初始化,不必擔心多線程問題。

故在性能需求較高時,應使用這種模式,避免頻繁的鎖爭奪。

class SingletonStatic
{
private:
  static const SingletonStatic* m_instance;
  SingletonStatic(){}
public:
  static const SingletonStatic* getInstance()
  {
    return m_instance;
  }
};
 
//外部初始化 before invoke main
const SingletonStatic* SingletonStatic::m_instance = new SingletonStatic;


boost庫的實現(xiàn)示例

單例本來是個很簡單的模式,實現(xiàn)上應該也是很簡單,但C++單例的簡單實現(xiàn)會有一些坑,有了上面線程安全的基礎,下面來看看為了避免這些坑怎樣一步步演化到boost庫的實現(xiàn)方式。

方案一

class QMManager
{
public:
  static QMManager &instance()
  {
    static QMManager instance_;
    return instance_;
  }
}

這是最簡單的版本,在單線程下(或者是C++0X下)是沒任何問題的,但在多線程下就不行了,因為static QMManager instance_;這句話不是線程安全的。
在局部作用域下的靜態(tài)變量在編譯時,編譯器會創(chuàng)建一個附加變量標識靜態(tài)變量是否被初始化,會被編譯器變成像下面這樣(偽代碼):

static QMManager &instance()
{
  static bool constructed = false;
  static uninitialized QMManager instance_;
  if (!constructed) {
    constructed = true;
    new(&s) QMManager; //construct it
  }
  return instance_;
}

這里有競爭條件,兩個線程同時調(diào)用instance()時,一個線程運行到if語句進入后還沒設constructed值,此時切換到另一線程,constructed值還是false,同樣進入到if語句里初始化變量,兩個線程都執(zhí)行了這個單例類的初始化,就不再是單例了。

方案二
一個解決方法是加鎖:

static QMManager &instance()
{
  Lock(); //鎖自己實現(xiàn)
  static QMManager instance_;
  UnLock();
  return instance_;
}

但這樣每次調(diào)用instance()都要加鎖解鎖,代價略大。

方案三
那再改變一下,把內(nèi)部靜態(tài)實例變成類的靜態(tài)成員,在外部初始化,也就是在include了文件,main函數(shù)執(zhí)行前就初始化這個實例,就不會有線程重入問題了:

class QMManager
{
protected:
  static QMManager instance_;
  QMManager();
  ~QMManager(){};
public:
  static QMManager *instance()
  {
    return &instance_;
  }
  void do_something();
};
QMManager QMManager::instance_; //外部初始化

這被稱為餓漢模式,程序一加載就初始化,不管有沒有調(diào)用到。
看似沒問題,但還是有坑,在一個2B情況下會有問題:在這個單例類的構造函數(shù)里調(diào)用另一個單例類的方法可能會有問題。
看例子:

//.h
class QMManager
{
protected:
  static QMManager instance_;
  QMManager();
  ~QMManager(){};
public:
  static QMManager *instance()
  {
    return &instance_;
  }
};
 
class QMSqlite
{
protected:
  static QMSqlite instance_;
  QMSqlite();
  ~QMSqlite(){};
public:
  static QMSqlite *instance()
  {
    return &instance_;
  }
  void do_something();
};
 
QMManager QMManager::instance_;
QMSqlite QMSqlite::instance_;

//.cpp
QMManager::QMManager()
{
  printf("QMManager constructor\n");
  QMSqlite::instance()->do_something();
}
 
QMSqlite::QMSqlite()
{
  printf("QMSqlite constructor\n");
}
void QMSqlite::do_something()
{
  printf("QMSqlite do_something\n");
}

這里QMManager的構造函數(shù)調(diào)用了QMSqlite的instance函數(shù),但此時QMSqlite::instance_可能還沒有初始化。
這里的執(zhí)行流程:程序開始后,在執(zhí)行main前,執(zhí)行到QMManager QMManager::instance_;這句代碼,初始化QMManager里的instance_靜態(tài)變量,調(diào)用到QMManager的構造函數(shù),在構造函數(shù)里調(diào)用QMSqlite::instance(),取QMSqlite里的instance_靜態(tài)變量,但此時QMSqlite::instance_還沒初始化,問題就出現(xiàn)了。
那這里會crash嗎,測試結果是不會,這應該跟編譯器有關,靜態(tài)數(shù)據(jù)區(qū)空間應該是先被分配了,在調(diào)用QMManager構造函數(shù)前,QMSqlite成員函數(shù)在內(nèi)存里已經(jīng)存在了,只是還未調(diào)到它的構造函數(shù),所以輸出是這樣:

QMManager constructor
QMSqlite do_something
QMSqlite constructor

方案四
那這個問題怎么解決呢,單例對象作為靜態(tài)局部變量有線程安全問題,作為類靜態(tài)全局變量在一開始初始化,有以上2B問題,那結合下上述兩種方式,可以解決這兩個問題。boost的實現(xiàn)方式是:單例對象作為靜態(tài)局部變量,但增加一個輔助類讓單例對象可以在一開始就初始化。如下:

//.h
class QMManager
{
protected:
  struct object_creator
  {
    object_creator()
    {
      QMManager::instance();
    }
    inline void do_nothing() const {}
  };
  static object_creator create_object_;
 
  QMManager();
  ~QMManager(){};
public:
  static QMManager *instance()
  {
    static QMManager instance;
    return &instance;
  }
};
QMManager::object_creator QMManager::create_object_;
 
class QMSqlite
{
protected:
  QMSqlite();
  ~QMSqlite(){};
  struct object_creator
  {
    object_creator()
    {
      QMSqlite::instance();
    }
    inline void do_nothing() const {}
  };
  static object_creator create_object_;
public:
  static QMSqlite *instance()
  {
    static QMSqlite instance;
    return &instance;
  }
  void do_something();
};
 
QMManager::object_creator QMManager::create_object_;
QMSqlite::object_creator QMSqlite::create_object_;

結合方案3的.cpp,這下可以看到正確的輸出和調(diào)用了:

QMManager constructor
QMSqlite constructor
QMSqlite do_something

來看看這里的執(zhí)行流程:
初始化QMManager類全局靜態(tài)變量create_object_
->調(diào)用object_creator的構造函數(shù)
->調(diào)用QMManager::instance()方法初始化單例
->執(zhí)行QMManager的構造函數(shù)
->調(diào)用QMSqlite::instance()
->初始化局部靜態(tài)變量QMSqlite instance
->執(zhí)行QMSqlite的構造函數(shù),然后返回這個單例。
跟方案三的區(qū)別在于QMManager調(diào)用QMSqlite單例時,方案3是取到全局靜態(tài)變量,此時這個變量未初始化,而方案四的單例是靜態(tài)局部變量,此時調(diào)用會初始化。
跟最初方案一的區(qū)別是在main函數(shù)前就初始化了單例,不會有線程安全問題。

最終boost
上面為了說明清楚點去除了模版,實際使用是用模版,不用寫那么多重復代碼,這是boost庫的模板實現(xiàn):

template <typename T>
struct Singleton
{
  struct object_creator
  {
    object_creator(){ Singleton<T>::instance(); }
    inline void do_nothing()const {}
  };
 
  static object_creator create_object;
 
public:
  typedef T object_type;
  static object_type& instance()
  {
    static object_type obj;
    //據(jù)說這個do_nothing是確保create_object構造函數(shù)被調(diào)用
    //這跟模板的編譯有關
    create_object.do_nothing();
    return obj;
  }
 
};
template <typename T> typename Singleton<T>::object_creator Singleton<T>::create_object;
 
class QMManager
{
protected:
  QMManager();
  ~QMManager(){};
  friend class Singleton<QMManager>;
public:
  void do_something(){};
};
 
int main()
{
  Singleton<QMManager>::instance()->do_something();
  return 0;
}

其實Boost庫這樣的實現(xiàn)像打了幾個補丁,用了一些特殊技巧,雖然確實繞過了坑實現(xiàn)了需求,但感覺挺不好的。

相關文章

  • C語言入門學習筆記之typedef簡介

    C語言入門學習筆記之typedef簡介

    typedef為C語言的關鍵字,作用是為一種數(shù)據(jù)類型定義一個新名字,下面這篇文章主要給大家介紹了關于C語言入門學習筆記之typedef簡介的相關資料,需要的朋友可以參考下
    2021-11-11
  • Qt中TableView與TreeView組件聯(lián)動實現(xiàn)

    Qt中TableView與TreeView組件聯(lián)動實現(xiàn)

    本文主要介紹了Qt中TableView與TreeView組件聯(lián)動實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-12-12
  • 徹底掌握C語言strcpy函數(shù)的用法

    徹底掌握C語言strcpy函數(shù)的用法

    C語言中的strcpy函數(shù),是一種C語言的標準庫函數(shù),它用于對字符串進行復制。本章帶你了解它的使用并模擬實現(xiàn)它
    2022-05-05
  • C++實現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找

    C++實現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找

    這篇文章主要介紹了C++實現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找方法,涉及數(shù)組的操作,有值得借鑒的技巧,需要的朋友可以參考下
    2014-09-09
  • C++實現(xiàn)LeetCode(200.島嶼的數(shù)量)

    C++實現(xiàn)LeetCode(200.島嶼的數(shù)量)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(200.島嶼的數(shù)量),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 最大對稱字符串的算法

    最大對稱字符串的算法

    題目:輸入一個字符串,輸出該字符串中對稱的子字符串的最大長度。比如輸入字符串“google”,由于該字符串里最長的對稱子字符串是“goog”,因此輸出4。
    2013-03-03
  • 深入了解C語言的動態(tài)內(nèi)存管理

    深入了解C語言的動態(tài)內(nèi)存管理

    所謂動態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文將用5600字帶你深入了解動態(tài)內(nèi)存管理,感興趣的可以學習一下
    2022-07-07
  • C++淺析引用的定義與使用

    C++淺析引用的定義與使用

    引用是C++一個很重要的特性,顧名思義是某一個變量或?qū)ο蟮膭e名,對引用的操作與對其所綁定的變量或?qū)ο蟮牟僮魍耆葍r,這篇文章主要給大家總結介紹了C++中引用的相關知識點,需要的朋友可以參考下
    2022-07-07
  • 利用C語言實現(xiàn)簡易版掃雷

    利用C語言實現(xiàn)簡易版掃雷

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡易版掃雷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • C++17中std::string_view的使用

    C++17中std::string_view的使用

    std::string_view是C++17標準庫中的一種新類型,它提供了對一個字符序列的非擁有式視圖,本文主要介紹了C++17中std::string_view的使用,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01

最新評論