C++/CLI中单件模板的实现

C++/CLI中单件模板的实现

template<typename _T> public ref class Singleton : public _T
  {
  public:
   static _T^ Instance(void)
   {
    if (_instance == nullptr)
    {
     _mut->WaitOne();
     try
     {
      if (_instance == nullptr)
      {
       _instance = safe_cast<_T^>(gcnew Singleton<_T>);
      }
     }
     finally
     {
      _mut->ReleaseMutex();
     }
    }
    return _instance;
   }
  protected:
   Singleton(void){}
  protected:
   static _T^ _instance = nullptr;
   static System::Threading::Mutex^ _mut = gcnew System::Threading::Mutex();
  };

你可能感兴趣的:(C++/CLI中单件模板的实现)