Skip to content

Commit 20e260e

Browse files
committed
1 parent 8f05d20 commit 20e260e

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Server/Server/Singleton.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#ifndef _SINGLETON_H_FILE
22
#define _SINGLETON_H_FILE
33
#include "CriticalSection.h"
4+
#include "Lock.h"
45

56
template<typename T>
6-
class Singleton : CriticalSection
7+
class Singleton
78
{
89
private:
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(){};
2426
public:
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

4143
template <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

Server/Server/Thread.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ Thread::Thread() :
1111
Thread::~Thread()
1212
{
1313
}
14-
bool Thread::IsRunning() const
15-
{
16-
return _IsRunning;
17-
}
1814
void 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
}

Server/Server/Thread.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
}
2023
protected:
2124
static DWORD _stdcall ThreadFunc(void* p);
2225
};

0 commit comments

Comments
 (0)