标签:st3 UNC instance 函数 dstar order -- 单例 ati
////////////////////////////////////////////////////////////////////////////////////////////////////////
// 4.单例模式测试
extern CMutex g_stMutex; // 这个在ThreadTest.cpp中定义了,这个声明一下!!!! class CSingletonTest { public: CSingletonTest() :m_iCount(0){} ~CSingletonTest(){} void AddCount() { m_iCount++; } void SubCount() { m_iCount--; } int GetCount() { return m_iCount; } private: int m_iCount; }; class CSingleton_1 { protected: // 禁止外部调用这几个函数 CSingleton_1(){} ~CSingleton_1(){} CSingleton_1(const CSingleton_1&); CSingleton_1& operator=(const CSingleton_1&); }; /////////////////////////////////////////////////////// // 懒汉模式 //类加载进来,没有对象,只有调用了getInstance方法时,才会创建对象。 //延迟加载形式。 并发过程中存在安全隐患。 template <typename TYPE> class CSingleton_2 : public CSingleton_1 { public: static TYPE* GetInstance() { if (NULL == m_pSingleton) { cout << "Try Lock --------------->" << endl; m_pSingleton = new CSingleton_2<TYPE>(); } return &m_pSingleton->m_stInstance; } private: static CSingleton_2<TYPE>* m_pSingleton; TYPE m_stInstance; private: // 自动释放内存(不需要定义也可以,程序结束是系统会自动回收) class CGarbo { public: ~CGarbo() { if (m_pSingleton) { delete m_pSingleton; m_pSingleton = NULL; } } }; static CGarbo garbo; }; template <typename TYPE> CSingleton_2<TYPE>* CSingleton_2<TYPE>::m_pSingleton; typedef CSingleton_2<CSingletonTest> SingletonTesHEAP_TYPE; //////////////////////////////////////////////////////////// // 加锁的懒汉实现 template <typename TYPE> class CSingleton_3 : public CSingleton_1 { public: static TYPE* GetInstance() { if (NULL == m_pSingleton) { cout << "Try Lock --------------->" << endl; CLock lock(g_stMutex); if (NULL == m_pSingleton) { m_pSingleton = new CSingleton_3<TYPE>(); } } return &m_pSingleton->m_stInstance; } private: static CSingleton_3<TYPE>* m_pSingleton; TYPE m_stInstance; }; template <typename TYPE> CSingleton_3<TYPE>* CSingleton_3<TYPE>::m_pSingleton; typedef CSingleton_3<CSingletonTest> SingletonTest3; //////////////////////////////////////////////////////////////// // 饿汉模式 template <typename TYPE> class CSingleton_4 : public CSingleton_1 { public: static TYPE* GetInstance() { //return &(const_cast<CSingleton2<TYPE>*>(m_pSingleton)->m_stInstance); return &(const_cast<CSingleton_4<TYPE>*>(m_pSingleton)->m_stInstance); } private: const static CSingleton_4<TYPE>* m_pSingleton; TYPE m_stInstance; }; template <typename TYPE> const CSingleton_4<TYPE>* CSingleton_4<TYPE>::m_pSingleton = new CSingleton_4<TYPE>; typedef CSingleton_4<CSingletonTest> SingletonTest4; //////////////////////////////////////////////////////////////////////// // 多线程测试函数 template <typename TYPE> class CSingletonThreadTest : public CThread { public: CSingletonThreadTest(const string& str) :m_szThreadName(str) {} ~CSingletonThreadTest(){} virtual void Run() { for (int i = 0; i < 5000; i++) { //CLock Lock(g_stMutex); TYPE::GetInstance()->AddCount(); //cout << m_szThreadName << ": " << TYPE::GetInstance()->GetCount() << endl; } } private: string m_szThreadName; }; void CSingleton_2TestFunc() { cout << "\n\n --------------- CSingleton_2TestFunc Start -------------->" << endl; double dStart = clock(); // 1.单线程测试 CSingletonTest* pTest = SingletonTesHEAP_TYPE::GetInstance(); pTest->AddCount(); cout << "pTest Count = " << pTest->GetCount() << endl; // pTest 和 pTesHEAP_TYPE指向同一个对象 CSingletonTest* pTesHEAP_TYPE = SingletonTesHEAP_TYPE::GetInstance(); pTesHEAP_TYPE->AddCount(); cout << "pTesHEAP_TYPE Count = " << pTesHEAP_TYPE->GetCount() << endl; // 2.多线程下测试 测试5000数据,加锁情况下5000, 不加锁不一定为5000 CSingletonThreadTest<SingletonTesHEAP_TYPE> Test1("Test1"); Test1.Start(); CSingletonThreadTest<SingletonTesHEAP_TYPE> TesHEAP_TYPE("TesHEAP_TYPE"); TesHEAP_TYPE.Start(); CSingletonThreadTest<SingletonTesHEAP_TYPE> Test3("Test3"); Test3.Start(); CSingletonThreadTest<SingletonTesHEAP_TYPE> Test4("Test4"); Test4.Start(); CSingletonThreadTest<SingletonTesHEAP_TYPE> Test5("Test5"); Test5.Start(); CT_SLEEP(1); cout << "CSingleton_2TestFunc : " << SingletonTesHEAP_TYPE::GetInstance()->GetCount() << endl; cout << "花费时间: " << (clock() - dStart) / CONST_1000 << endl; cout << "\n\n --------------- CSingleton_2TestFunc End -------------->" << endl; } template <typename TYPE> void SingletonTestFunc() { //double dStart = clock(); // 1.单线程测试 TYPE::GetInstance()->AddCount(); TYPE::GetInstance()->AddCount(); cout << typeid(TYPE::GetInstance()).name() << ": Count = " << TYPE::GetInstance()->GetCount() << endl; CSingletonThreadTest<TYPE> Test1("Test1"); Test1.Start(); CSingletonThreadTest<TYPE> TesHEAP_TYPE("TesHEAP_TYPE"); TesHEAP_TYPE.Start(); CSingletonThreadTest<TYPE> Test3("Test3"); Test3.Start(); CSingletonThreadTest<TYPE> Test4("Test4"); Test4.Start(); CSingletonThreadTest<TYPE> Test5("Test5"); Test5.Start(); CT_SLEEP(10); cout << "SingletonTestFunc : " << TYPE::GetInstance()->GetCount() << endl; //cout << "花费时间: " << (clock() - dStart) / CONST_1000 << endl; } void SingletonPatternTestFunc() { cout << "\n\n --------------- SingletonPatternTestFunc Start -------------->" << endl; //CSingleton_2TestFunc(); SingletonTestFunc<SingletonTesHEAP_TYPE>(); cout << "===================================================>>>" << endl; SingletonTestFunc<SingletonTest3>(); cout << "===================================================>>>" << endl; SingletonTestFunc<SingletonTest4>(); cout << "\n\n --------------- SingletonPatternTestFunc End -------------->" << endl; }
标签:st3 UNC instance 函数 dstar order -- 单例 ati
原文地址:https://www.cnblogs.com/yzdai/p/11258589.html