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

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>
            日韩亚洲精品电影| 国产精品视频在线观看| 欧美一区二区在线视频| 免费欧美日韩| 久久夜色精品一区| 国产精品视频精品| 一区二区三区成人| 亚洲美女中出| 免费精品视频| 卡一卡二国产精品| 国产一区白浆| 亚洲午夜小视频| 亚洲线精品一区二区三区八戒| 鲁大师影院一区二区三区| 久久亚洲精品一区| 国产一级久久| 欧美一区高清| 久久久久久久一区| 国产一区二区高清视频| 欧美亚洲一区二区在线| 久久精品欧美日韩| 国产欧美综合一区二区三区| 亚洲视频在线观看三级| 亚洲在线观看视频| 国产精品色午夜在线观看| 99视频超级精品| 亚洲一区二区精品在线| 欧美日韩视频一区二区三区| 亚洲九九精品| 亚洲综合三区| 国产欧美va欧美不卡在线| 亚洲女ⅴideoshd黑人| 欧美在线视频免费观看| 国产一区二区你懂的| 久久国产精品亚洲va麻豆| 浪潮色综合久久天堂| 影音先锋日韩资源| 欧美成人网在线| 亚洲精选在线| 欧美一区二区三区另类| 国产综合在线视频| 久久综合五月| 91久久在线观看| 亚洲午夜在线视频| 国产日韩欧美高清免费| 久久精品国产成人| 亚洲韩国青草视频| 亚洲中字黄色| 黄色一区二区在线| 欧美日韩播放| 欧美一区二区三区视频免费播放 | 欧美在线观看一区二区| 国产综合色在线| 欧美高清一区| 亚洲欧美久久久久一区二区三区| 久久精品视频导航| 亚洲精品日韩在线观看| 国产精品区二区三区日本| 久久国产乱子精品免费女 | 欧美黄色免费网站| 亚洲一区欧美一区| 亚洲高清不卡在线观看| 欧美一区二区三区另类| 最新中文字幕一区二区三区| 欧美三级日本三级少妇99| 久久精品国产久精国产爱| 亚洲精品免费电影| 六月丁香综合| 欧美在线免费| 一区二区三区成人精品| 精品二区久久| 国产精品主播| 欧美国产视频日韩| 欧美一区久久| 在线视频日本亚洲性| 欧美成年人视频网站欧美| 亚洲欧美综合国产精品一区| 午夜精品影院| 日韩视频一区二区在线观看 | 亚洲与欧洲av电影| 欧美成人亚洲| 久久不射网站| 99国产精品久久| 在线观看精品一区| 国产精品一区二区三区乱码| 欧美顶级大胆免费视频| 欧美在线视频观看免费网站| av成人毛片| 亚洲精品久久久久中文字幕欢迎你| 久久久久免费| 久久精品国产亚洲一区二区三区| 亚洲视频网在线直播| 亚洲茄子视频| 亚洲观看高清完整版在线观看| 国产精一区二区三区| 欧美日韩精品一区二区三区四区| 免费不卡在线观看| 老司机一区二区三区| 久久久7777| 欧美一区在线视频| 欧美亚洲免费| 性欧美videos另类喷潮| 亚洲欧美激情四射在线日| 亚洲小说春色综合另类电影| 一区二区三区回区在观看免费视频 | 亚洲欧洲精品一区二区三区不卡| 国产日韩欧美在线观看| 国产精品一区亚洲| 国产精品专区一| 国产噜噜噜噜噜久久久久久久久| 欧美日韩在线播放一区二区| 欧美日韩精品一区二区| 欧美色网一区二区| 国产精品久久77777| 国产精品扒开腿爽爽爽视频 | 国产精品红桃| 国产精品久久中文| 国产欧美精品一区二区色综合| 国产精品色在线| 国产三级精品三级| 一区免费观看| 亚洲精品欧美日韩专区| 一区二区三区日韩精品| 亚洲免费婷婷| 久久精品一区二区三区不卡| 久久亚洲影院| 欧美韩日精品| 99ri日韩精品视频| 午夜在线视频一区二区区别| 久久精品国产亚洲aⅴ| 六月婷婷一区| 欧美日韩中文字幕在线| 国产精品腿扒开做爽爽爽挤奶网站| 国产欧美在线看| 亚洲福利在线视频| 日韩视频一区二区| 欧美在线播放| 亚洲高清网站| 在线视频精品| 久久免费观看视频| 欧美日韩免费看| 国产专区一区| 99综合在线| 久久先锋资源| 99视频精品| 久久深夜福利免费观看| 欧美日韩亚洲国产一区| 国产一区激情| 中文精品视频一区二区在线观看| 久久久99国产精品免费| 亚洲人成人一区二区三区| 欧美在线高清视频| 欧美日韩免费观看一区二区三区| 国产一区二区三区成人欧美日韩在线观看 | 亚洲精品国产拍免费91在线| 亚洲嫩草精品久久| 亚洲成人在线视频播放 | 久久人91精品久久久久久不卡| 欧美成人精品在线| 国产亚洲电影| 亚洲视频一区在线| 欧美韩日一区二区三区| 亚洲欧美综合v| 欧美日韩精品在线观看| 亚洲福利视频网| 久久久99国产精品免费| 亚洲婷婷在线| 欧美精品成人| 亚洲欧洲日本专区| 久久精品网址| 亚洲香蕉成视频在线观看| 欧美国产三区| 亚洲精品免费电影| 久久亚洲欧洲| 午夜欧美理论片| 国产精品久久久久久户外露出| 亚洲精品免费在线播放| 免费观看欧美在线视频的网站| 亚洲欧美电影在线观看| 欧美性色综合| 亚洲天堂免费观看| 亚洲精品视频一区| 欧美黑人在线播放| 日韩一级二级三级| 亚洲破处大片| 欧美久久视频| 夜夜嗨av色综合久久久综合网| 亚洲国产精品一区| 欧美激情亚洲精品| 一本色道久久综合亚洲精品按摩 | 亚洲一区尤物| 一区二区三区日韩欧美| 欧美日韩亚洲综合在线| 亚洲一区二区三区四区五区午夜 | 欧美韩国在线| 欧美成人精品三级在线观看| 亚洲国产精品一区二区www| 免费欧美视频| 免费久久久一本精品久久区| 亚洲第一区在线观看|