- CriticalSection 의 Enter, Leave 를 지역변수로 선언하여 데드락을 피하는 클래스
1. CRITICAL_SECTION의 기능형 클래스
2. Enter, Leave를 지역변수화.
1. CRITICAL_SECTION의 기능형 클래스
#pragma once
class CCriticalSection
{
public:
CCriticalSection(VOID)
{
InitializeCriticalSection(&mSync);
}
~CCriticalSection(VOID)
{
DeleteCriticalSection(&mSync);
}
inline VOID Enter(VOID)
{
EnterCriticalSection(&mSync);
}
inline VOID Leave(VOID)
{
LeaveCriticalSection(&mSync);
}
private:
CRITICAL_SECTION mSync;
};
class CCriticalSection
{
public:
CCriticalSection(VOID)
{
InitializeCriticalSection(&mSync);
}
~CCriticalSection(VOID)
{
DeleteCriticalSection(&mSync);
}
inline VOID Enter(VOID)
{
EnterCriticalSection(&mSync);
}
inline VOID Leave(VOID)
{
LeaveCriticalSection(&mSync);
}
private:
CRITICAL_SECTION mSync;
};
2. Enter, Leave를 지역변수화.
#pragma once
template <class T>
class CMultiThreadSync
{
friend class CThreadSync;
public:
class CThreadSync
{
public:
CThreadSync(VOID)
{
T::mSync.Enter();
}
~CThreadSync(VOID)
{
T::mSync.Leave();
}
};
private:
static CCriticalSection mSync;
};
template <class T>
CCriticalSection CMultiThreadSync<T>::mSync;
template <class T>
class CMultiThreadSync
{
friend class CThreadSync;
public:
class CThreadSync
{
public:
CThreadSync(VOID)
{
T::mSync.Enter();
}
~CThreadSync(VOID)
{
T::mSync.Leave();
}
};
private:
static CCriticalSection mSync;
};
template <class T>
CCriticalSection CMultiThreadSync<T>::mSync;
- 사용 예제 ( 메모리 풀 )
more..