File tree Expand file tree Collapse file tree 3 files changed +15
-11
lines changed
Expand file tree Collapse file tree 3 files changed +15
-11
lines changed Original file line number Diff line number Diff line change 11#ifndef _SINGLETON_H_FILE
22#define _SINGLETON_H_FILE
33#include " CriticalSection.h"
4+ #include " Lock.h"
45
56template <typename T>
6- class Singleton : CriticalSection
7+ class Singleton
78{
89private:
9- Singleton (){};
1010 static T* _Instance;
11-
11+ static CriticalSection _CS;
1212 class MyClass
1313 {
1414 public:
@@ -21,13 +21,15 @@ class Singleton : CriticalSection
2121 }
2222 };
2323 static MyClass _Harmonior;
24+ protected:
25+ Singleton (){};
2426public:
2527 virtual ~Singleton (){};
2628 static T* GetInstance ()
2729 {
2830 if (_Instance == nullptr )
2931 {
30- Lock ( this );
32+ Lock l (&_CS );
3133 if (_Instance == nullptr )
3234 {
3335 _Instance = new T ();
@@ -39,7 +41,10 @@ class Singleton : CriticalSection
3941};
4042
4143template <typename T>
42- T Singleton<T>::_Instance = nullptr ;
44+ T* Singleton<T>::_Instance = nullptr ;
45+
46+ template <typename T>
47+ CriticalSection Singleton<T>::_CS;
4348
4449
4550#endif // !_SINGLETON_H_FILE
Original file line number Diff line number Diff line change @@ -11,10 +11,6 @@ Thread::Thread() :
1111Thread::~Thread ()
1212{
1313}
14- bool Thread::IsRunning () const
15- {
16- return _IsRunning;
17- }
1814void Thread::Init (IRunnable *runfuc)
1915{
2016 assert (_Runnable == nullptr || runfuc != nullptr );
@@ -55,9 +51,9 @@ DWORD Thread::ThreadFunc(void* p)
5551 Thread* pThread = static_cast <Thread*>(p);
5652 if (pThread != nullptr )
5753 {
54+ IRunnable* runnable = pThread->_Runnable ;
5855 while (pThread->_IsRunning )
5956 {
60- IRunnable* runnable = pThread->_Runnable ;
6157 runnable->Run ();
6258 }
6359 }
Original file line number Diff line number Diff line change @@ -16,7 +16,10 @@ class Thread
1616 void Suspend ();
1717 void Resume ();
1818 void Stop ();
19- inline bool IsRunning () const ;
19+ inline bool IsRunning () const
20+ {
21+ return _IsRunning;
22+ }
2023protected:
2124 static DWORD _stdcall ThreadFunc (void * p);
2225};
You can’t perform that action at this time.
0 commit comments