网络服务器软件开发/中间件开发,关注ACE/ICE/boost

C++博客 首页 新随笔 联系 聚合 管理
  152 Posts :: 3 Stories :: 172 Comments :: 0 Trackbacks

//资源保护对象
class CAutoLock
{
public:
 CAutoLock(){ ::InitializeCriticalSection(&m_crit);}
 virtual ~CAutoLock() { ::DeleteCriticalSection(&m_crit); }
public:
 void  Lock(){  ::EnterCriticalSection(&m_crit); }
 void  UnLock(){ ::LeaveCriticalSection(&m_crit); }
private:
 CRITICAL_SECTION  m_crit;
};
template <class T>
class SingleTon
{
public:
 SingleTon(){};
 ~SingleTon(){ if( instance_ ) delete instance_; }
public:
 static T* get_instance()
 {
  if( instance_ == 0 )
  {
   lock_.Lock();
   try
   {
    if( instance_ == 0 )
     instance_ = new T;
   }
   catch (...)
   {

   }
   lock_.UnLock();
  }

  return instance_;
 }

protected:
 static CAutoLock lock_ ;
 static T* instance_;
};


template <class T>
CAutoLock SingleTon<T>::lock_;
template<class T>
T* SingleTon<T>::instance_;

posted on 2007-06-04 19:29 true 阅读(367) 评论(3)  编辑 收藏 引用 所属分类: C++基础

Feedback

# re: 单体--【来自群友】 2009-03-17 20:17 晓清
文化交流  回复  更多评论
  

# re: 单体--【来自群友】 2009-03-17 20:20 晓清
思想交流,情感交流。  回复  更多评论
  

# re: 单体--【来自群友】 2009-03-17 20:24 晓清
主要讲述看法观点多加评论。  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理