青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Where there is a dream ,there is hope

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

溫故而知新,總結了多種單例模式的寫法,考慮上多線程沒有非常完美的,各取所需吧

//1.
//Easy ,<<Pattern Design>>
//Q:
//When to free the m_singleton space?
//when to execute the destruct function?
//A:We can call getSingleton() at the end of the progress, 
//then delete the pointer and free the space, 
//but ,Here is a problem:Someone will call getSingleton() after 
//we delete it。How to avoid these?
class Singleton
{
    
public:
        
static Singleton* getSingleton()
        
{
            
if(NULL==m_singleton)
                
{
                    m_singleton
=new Singleton();
                }

                
return m_singleton;
        }

    
private:
        Singleton()
{}
        
~Singleton(){}
    
private:
        
static Singleton* m_singleton;        
}
;
//singleton.cpp
singleton* Singleton::m_singleton=NULL;
//Because ,at the end of the program, all globle vars will be destructed. ,
//and all the static vars .we can use this point, define a static var in
//the Singleton class ,it is used to delete the Singleton class .

//2.-----------
class Singleton
{
    
public:
        staitc Singleton
* getSingleton();
    
private:
        Singleton();
        
~Singleton();
        
static Singleton* m_singleton;
        
class Garbo//Just used for delete the static var
        {
            
public:
                Garbo();
                
~Garbo()
                
{
                    
if(Singleton::m_singleton!=NULL)
                        
{
                            delete Singleton::m_singleton;
                            Singleton::m_singleton
=NULL;
                      }

                }

        }

        staitc Garbo m_garbo;
//define a static var,
}

//singleton.cpp
Garbo Singleton::m_garbo;
//

//3.---------------
class Singleton
{
    
public:
    staitc Singleton
* getSingletonPtr()
    
{
        
if(NULL==m_singleton)
            
{
                m_singleton.reset(
new Singleton);
            }

            
return m_singleton.get();
    }

    
protected:
        Singleton()
{}
        
virtual ~Singleton(){}
        
        friend 
class auto_ptr<Singleton>;
        
static auto_ptr<Singleton> m_singleton;
}
;

//singleton.cpp
auto_ptr<Singleton> Singleton::m_singleton=NULL;
    


//4.
class Singleton
{
    
public:
        
static Singleton* getSingletonPtr()
        
{
            staitc Singleton singleton;
            
return &singleton;
        }

    
private:
        Singleton()
{}
        
~Singleton(){}
}


//5.
class Singleton
{
    
public:
        staitc Singleton
& getSingleton
        
{
            
return m_singleton;
        }

    
private:
        staitc Singleton m_singleton;
        Singleton()
{}
        
}



//6.---------thread safe

#pragma once
 
#include 
<memory>
using namespace std;
#include 
"Interlocked.h"
using namespace C2217::Win32;
 
namespace C2217
{
namespace Pattern
{
template 
<class T>
class Singleton
{
public:
       
static inline T* instance();
      
private:
       Singleton(
void){}
       
~Singleton(void){}
       Singleton(
const Singleton&){}
       Singleton 
& operator= (const Singleton &){}
 
       
static auto_ptr<T> _instance;
       
static CResGuard _rs;
}
;
 
template 
<class T>
auto_ptr
<T> Singleton<T>::_instance;
 
template 
<class T>
CResGuard Singleton
<T>::_rs;
 
template 
<class T>
 inline T
* Singleton<T>::instance()
{
       
if0 == _instance.get() )
       
{
              CResGuard::CGuard gd(_rs);
              
if0== _instance.get())
              
{
                     _instance.reset ( 
new T);
              }

       }

       
return _instance.get();
}

 
//Class that will implement the singleton mode,
//must use the macro in it's delare file
#define DECLARE_SINGLETON_CLASS( type ) \
       friend 
class auto_ptr< type >;\
       friend 
class Singleton< type >;
}

}

//       CresGuard 類主要的功能是線程訪問同步,代碼如下:
/******************************************************************************

*****************************************************************************
*/

 
 
#pragma once
///////////////////////////////////////////////////////////////////////////////
 
// Instances of this class will be accessed by multiple threads. So,
// all members of this class (except the constructor and destructor)
// must be thread-safe.
class CResGuard {
public:
   CResGuard()  
{ m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }
   
~CResGuard() { DeleteCriticalSection(&m_cs); }
 
   
// IsGuarded is used for debugging
   BOOL IsGuarded() const return(m_lGrdCnt > 0); }
 
public:
   
class CGuard {
   
public:
      CGuard(CResGuard
& rg) : m_rg(rg) { m_rg.Guard(); };
      
~CGuard() { m_rg.Unguard(); }
 
   
private:
      CResGuard
& m_rg;
   }
;
 
private:
   
void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }
   
void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }
 
   
// Guard/Unguard can only be accessed by the nested CGuard class.
   friend class CResGuard::CGuard;
 
private:
   CRITICAL_SECTION m_cs;
   
long m_lGrdCnt;   // # of EnterCriticalSection calls
}
;

//7. most used
template <typename T>
class Singleton
{
public :
    
static T& getSingleton()
    
{
        
return *m_singlon;
    }


    
static T* getSingletonPtr()
    
{
        
return m_singlon;
    }

protected:

    Singleton()
    
{
        m_singlon
=static_cast<T*>(this);
    }


    
~Singleton()
    
{
        m_singlon
=NULL;
    }

private:
    Singleton(
const Singleton&){}

    Singleton
& operator=(const Singleton&){}

    
static T* m_singlon;
}
;

template
<typename T>
T
* Singleton<T>::m_singlon = NULL;
posted on 2011-03-09 16:18 IT菜鳥 閱讀(339) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲精品久久久久久久久久久| 韩国欧美国产1区| 国产主播一区| 久久久91精品国产| 一本色道久久综合亚洲精品不卡| 亚洲激情欧美激情| 久久福利视频导航| 亚洲综合成人婷婷小说| 国产精品草莓在线免费观看| 在线观看的日韩av| 欧美大片免费| 欧美成人一品| 一区二区三区三区在线| 日韩视频不卡| 国产精品扒开腿爽爽爽视频 | 亚洲电影毛片| 欧美黄色网络| 欧美偷拍另类| 欧美亚洲免费高清在线观看| 香蕉成人伊视频在线观看| 国产日韩欧美在线一区| 久久乐国产精品| 欧美成人日本| 亚洲福利视频一区二区| 欧美区高清在线| 午夜精品国产更新| 久久久亚洲午夜电影| 亚洲片在线观看| 亚洲永久免费| 99re国产精品| 久久成人精品视频| 亚洲看片一区| 久久精品视频免费| 亚洲永久免费观看| 久久午夜av| 亚洲在线一区二区| 国产精品嫩草影院一区二区| 欧美暴力喷水在线| 国产精品久久久久三级| 亚洲综合电影| 久久深夜福利免费观看| 亚洲缚视频在线观看| 午夜在线一区二区| 久久久青草青青国产亚洲免观| 久久国产日韩| 欧美一级淫片aaaaaaa视频| 国产精品一区久久久| 亚洲国产老妈| 好吊一区二区三区| 久久精品成人一区二区三区 | 久久国产精品电影| 久久人人爽人人爽| 在线免费日韩片| 欧美成人dvd在线视频| 亚洲欧洲在线一区| 99精品国产在热久久| 国产精品成人免费| 亚洲高清成人| 一区二区日韩伦理片| 国产精品久久久一区二区| 欧美一区国产二区| 午夜精品福利在线| 激情久久综艺| 久久成人这里只有精品| 亚洲二区在线观看| 欧美一区不卡| 国产性猛交xxxx免费看久久| 久热这里只精品99re8久| 亚洲综合三区| 亚洲欧洲视频在线| 国产一区二区欧美日韩| 亚洲欧美第一页| 亚洲国产片色| 久久久亚洲欧洲日产国码αv| 国产精品永久| 欧美日韩精品一区二区天天拍小说| 日韩亚洲欧美高清| 国产一区亚洲| 欧美系列精品| 欧美激情一区二区三级高清视频 | 欧美一区二区三区喷汁尤物| 亚洲激情国产精品| 欧美福利影院| 日韩亚洲综合在线| 欧美成年人视频网站| 亚洲一区二区三区精品视频| 国产精品视频精品视频| 欧美一站二站| 亚洲黄色毛片| 女同一区二区| 久久裸体艺术| 欧美制服第一页| 亚洲图片激情小说| 国产综合久久久久久鬼色| 欧美色图一区二区三区| 欧美激情亚洲一区| 欧美成人蜜桃| 欧美gay视频激情| 老司机久久99久久精品播放免费| 欧美日韩国语| 猫咪成人在线观看| 一区二区av在线| 亚洲人成毛片在线播放女女| 中日韩男男gay无套| 日韩视频精品在线观看| 亚洲日本一区二区| 亚洲激情自拍| 亚洲免费av片| 这里是久久伊人| 亚洲欧美日韩专区| 激情综合色丁香一区二区| 国内一区二区三区| 依依成人综合视频| 亚洲黄色毛片| 一区二区av在线| 久久综合久久综合这里只有精品| 欧美黑人在线观看| 欧美国产精品va在线观看| 欧美jizz19hd性欧美| 美女日韩在线中文字幕| 亚洲免费激情| 亚洲午夜日本在线观看| 亚洲天堂成人| 久久精品国产亚洲精品| 亚洲天堂av图片| 性xx色xx综合久久久xx| 久久精品国产在热久久 | 欧美中文字幕不卡| 久久精品91| 久久三级视频| 亚洲国产精品一区| 99在线精品视频| 欧美黄色免费网站| 亚洲精品欧美精品| 欧美激情aⅴ一区二区三区| 91久久精品国产91性色| 亚洲一区二区三区四区视频| 欧美一区免费| 欧美日韩第一页| 国产日韩欧美在线观看| 亚洲欧洲精品一区二区| 亚洲欧美另类中文字幕| 欧美+亚洲+精品+三区| 一本色道久久88精品综合| 欧美亚洲网站| 欧美连裤袜在线视频| 国产精品尤物| 亚洲精品中文字幕在线| 欧美亚洲一级片| 亚洲国产精品一区二区第四页av| 欧美阿v一级看视频| 一区二区三区视频在线| 久久久高清一区二区三区| 欧美日韩一区二区欧美激情| 欧美日韩在线一二三| 极品少妇一区二区三区精品视频| 国产精品一区二区久久久久| 亚洲激情视频网| 久久精品99久久香蕉国产色戒| 亚洲欧美三级伦理| 亚洲电影免费观看高清完整版在线观看 | 免费观看日韩| 亚洲色图自拍| 一区二区三区视频在线观看| 久久国产精品久久久久久久久久 | 欧美一区二区三区的| 欧美精品1区2区| 一区二区三区在线看| 欧美一区二区三区在线观看| 日韩性生活视频| 乱码第一页成人| 国产亚洲激情在线| 午夜精品一区二区三区在线| 亚洲高清av在线| 久久久伊人欧美| 国产在线播放一区二区三区| 亚洲综合丁香| 在线一区二区视频| 欧美日韩国产一区二区| 亚洲精品欧美在线| 亚洲国产激情| 蜜桃久久精品乱码一区二区| 激情六月婷婷综合| 老司机午夜精品| 久久精品国产2020观看福利| 国产日韩av在线播放| 欧美日韩成人精品| 日韩亚洲国产精品| 亚洲国产欧美一区二区三区久久| 99re热精品| 欧美日本精品在线| 日韩视频一区二区三区| 亚洲黄网站在线观看| 欧美精品99| 亚洲午夜国产一区99re久久| 日韩一区二区久久| 欧美性jizz18性欧美| 欧美一级理论片| 欧美在线三级| 亚洲国产精品久久精品怡红院|