Реферат: Критические секции

#if CS_DEBUG > 1

void Lock(int nLine, LPSTR azFile) { EnterCriticalSectionDbg(&m_CS, nLine, azFile); }

BOOL TryLock(int nLine, LPSTR azFile) { return TryEnterCriticalSectionDbg(&m_CS, nLine, azFile); }

#else

void Lock() { ::EnterCriticalSection(&m_CS); }

BOOL TryLock() { return ::TryEnterCriticalSection(&m_CS); }

#endif

void Unlock() { ::LeaveCriticalSection(&m_CS); }

};

class CScopeLock

{

LPCRITICAL_SECTION m_pCS;

public:

#if CS_DEBUG > 1

CScopeLock(LPCRITICAL_SECTION pCS, int nLine, LPSTR azFile) : m_pCS(pCS) { Lock(nLine, azFile); }

CScopeLock(CLock& lock, int nLine, LPSTR azFile) : m_pCS(&lock.m_CS) { Lock(nLine, azFile); }

void Lock(int nLine, LPSTR azFile) { EnterCriticalSectionDbg(m_pCS, nLine, azFile); }

#else

CScopeLock(LPCRITICAL_SECTION pCS) : m_pCS(pCS) { Lock(); }

CScopeLock(CLock& lock) : m_pCS(&lock.m_CS) { Lock(); }

void Lock() { ::EnterCriticalSection(m_pCS); }

#endif

~CScopeLock() { Unlock(); }

void Unlock() { ::LeaveCriticalSection(m_pCS); }

};

#if CS_DEBUG > 1

#define Lock() Lock(__LINE__, __FILE__)

#define TryLock() TryLock(__LINE__, __FILE__)

#define lock(cs) lock(cs, __LINE__, __FILE__)

К-во Просмотров: 781
Бесплатно скачать Реферат: Критические секции