-
Notifications
You must be signed in to change notification settings - Fork 23
/
CritSect.h
51 lines (38 loc) · 1017 Bytes
/
CritSect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/******************************************************************************
File: CriticalSection.h
Author: Blair McGlashan
Description:
******************************************************************************/
#pragma once
class CMonitor
{
public:
CMonitor() {InitializeCriticalSection(&m_cs);}
~CMonitor() {DeleteCriticalSection(&m_cs);}
void Lock() {EnterCriticalSection(&m_cs);}
void Unlock() {LeaveCriticalSection(&m_cs);}
private:
// Suppress copy constructor and assignment operator
const CMonitor& operator=(const CMonitor&) = delete;
CMonitor(const CMonitor&) = delete;
private:
CRITICAL_SECTION m_cs;
};
template <class _T> class CAutoLock
{
_T& m_mutex;
private:
// Suppress copy constructor and assignment operator
const CAutoLock& operator=(const CAutoLock&) = delete;
CAutoLock(const CAutoLock&) = delete;
public:
CAutoLock(_T& mutex) : m_mutex(mutex)
{
m_mutex.Lock();
}
~CAutoLock()
{
m_mutex.Unlock();
}
};
typedef CAutoLock<CMonitor> CMonitorLock;