더북(TheBook)

코드 chapter_1\deadlock-example

class CriticalSection
{
    CRITICAL_SECTION m_critSec;
    public:
    CriticalSection();
    ~CriticalSection();
 
    void Lock();
    void Unlock();
};
 
class CriticalSectionLock
{
    CriticalSection* m_pCritSec;
    public:
    CriticalSectionLock(CriticalSection& critSec);
    ~CriticalSectionLock();
};
 
CriticalSection::CriticalSection()
{
    InitializeCriticalSectionEx(&m_critSec, 0, 0);
}
 
CriticalSection::~CriticalSection()
{
    DeleteCriticalSection(&m_critSec);
}
 
void CriticalSection::Lock()
{
    EnterCriticalSection(&m_critSec);
}
 
void CriticalSection::Unlock()
{
    LeaveCriticalSection(&m_critSec);
}
 
CriticalSectionLock::CriticalSectionLock(CriticalSection& critSec)
{
    m_pCritSec = &critSec;
    m_pCritSec->Lock();
}
 
CriticalSectionLock::~CriticalSectionLock()
{
    m_pCritSec->Unlock();
}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.