网络服务器软件开发

C++博客 首页 新随笔 联系 聚合 管理
  87 Posts :: 3 Stories :: 52 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 阅读(111) 评论(0)  编辑 收藏 引用 所属分类: C++基础

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航: