欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

老生常談C++的單例模式與線(xiàn)程安全單例模式(懶漢/餓漢)

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

1 教科書(shū)里的單例模式

我們都很清楚一個(gè)簡(jiǎn)單的單例模式該怎樣去實(shí)現(xiàn):構(gòu)造函數(shù)聲明為private或protect防止被外部函數(shù)實(shí)例化,內(nèi)部保存一個(gè)private static的類(lèi)指針保存唯一的實(shí)例,實(shí)例的動(dòng)作由一個(gè)public的類(lèi)方法代勞,該方法也返回單例類(lèi)唯一的實(shí)例。

上代碼: 

class singleton
{
protected:
  singleton(){}
private:
  static singleton* p;
public:
  static singleton* instance();
};
singleton* singleton::p = NULL;
singleton* singleton::instance()
{
  if (p == NULL)
    p = new singleton();
  return p;
}

這是一個(gè)很棒的實(shí)現(xiàn),簡(jiǎn)單易懂。但這是一個(gè)完美的實(shí)現(xiàn)嗎?不!該方法是線(xiàn)程不安全的,考慮兩個(gè)線(xiàn)程同時(shí)首次調(diào)用instance方法且同時(shí)檢測(cè)到p是NULL值,則兩個(gè)線(xiàn)程會(huì)同時(shí)構(gòu)造一個(gè)實(shí)例給p,這是嚴(yán)重的錯(cuò)誤!同時(shí),這也不是單例的唯一實(shí)現(xiàn)!

2 懶漢與餓漢

單例大約有兩種實(shí)現(xiàn)方法:懶漢與餓漢。

懶漢:故名思義,不到萬(wàn)不得已就不會(huì)去實(shí)例化類(lèi),也就是說(shuō)在第一次用到類(lèi)實(shí)例的時(shí)候才會(huì)去實(shí)例化,所以上邊的經(jīng)典方法被歸為懶漢實(shí)現(xiàn);

餓漢:餓了肯定要饑不擇食。所以在單例類(lèi)定義的時(shí)候就進(jìn)行實(shí)例化。

特點(diǎn)與選擇:

由于要進(jìn)行線(xiàn)程同步,所以在訪(fǎng)問(wèn)量比較大,或者可能訪(fǎng)問(wèn)的線(xiàn)程比較多時(shí),采用餓漢實(shí)現(xiàn),可以實(shí)現(xiàn)更好的性能。這是以空間換時(shí)間。

在訪(fǎng)問(wèn)量較小時(shí),采用懶漢實(shí)現(xiàn)。這是以時(shí)間換空間。

3 線(xiàn)程安全的懶漢實(shí)現(xiàn)

線(xiàn)程不安全,怎么辦呢?最直觀的方法:加鎖。

方法1:加鎖的經(jīng)典懶漢實(shí)現(xiàn):

class singleton
{
protected:
  singleton()
  {
    pthread_mutex_init(&mutex);
  }
private:
  static singleton* p;
public:
  static pthread_mutex_t mutex;
  static singleton* initance();
};

pthread_mutex_t singleton::mutex;
singleton* singleton::p = NULL;
singleton* singleton::initance()
{
  if (p == NULL)
  {
    pthread_mutex_lock(&mutex);
    if (p == NULL)
      p = new singleton();
    pthread_mutex_unlock(&mutex);
  }
  return p;
}

方法2:內(nèi)部靜態(tài)變量的懶漢實(shí)現(xiàn)

此方法也很容易實(shí)現(xiàn),在instance函數(shù)里定義一個(gè)靜態(tài)的實(shí)例,也可以保證擁有唯一實(shí)例,在返回時(shí)只需要返回其指針就可以了。推薦這種實(shí)現(xiàn)方法,真得非常簡(jiǎn)單。

class singleton
{
protected:
  singleton()
  {
    pthread_mutex_init(&mutex);
  }
public:
  static pthread_mutex_t mutex;
  static singleton* initance();
  int a;
};

pthread_mutex_t singleton::mutex;
singleton* singleton::initance()
{
  pthread_mutex_lock(&mutex);
  static singleton obj;
  pthread_mutex_unlock(&mutex);
  return &obj;
}

4 餓漢實(shí)現(xiàn)

為什么我不講“線(xiàn)程安全的餓漢實(shí)現(xiàn)”?因?yàn)轲I漢實(shí)現(xiàn)本來(lái)就是線(xiàn)程安全的,不用加鎖。為啥?自己想!

class singleton
{
protected:
  singleton()
  {}
private:
  static singleton* p;
public:
  static singleton* initance();
};
singleton* singleton::p = new singleton;
singleton* singleton::initance()
{
  return p;
}

是不是特別簡(jiǎn)單呢?

以空間換時(shí)間,你說(shuō)簡(jiǎn)單不簡(jiǎn)單?

面試的時(shí)候,線(xiàn)程安全的單例模式怎么寫(xiě)?肯定怎么簡(jiǎn)單怎么寫(xiě)呀!餓漢模式反而最懶[正經(jīng)臉]! 

以上就是小編為大家?guī)?lái)的老生常談C++的單例模式與線(xiàn)程安全單例模式(懶漢/餓漢)全部?jī)?nèi)容了,希望大家多多支持我們~

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有