windows平台下的锁
1 /// 锁,利用临界区实现
2 class ZLock{
3 CRITICAL_SECTION m_CS ;
4 public:
5 ZLock(){
6 ::InitializeCriticalSection(&m_CS) ;
7 }
8
9 ~ZLock(){
10 ::DeleteCriticalSection(&m_CS) ;
11 }
12
13 void Lock()
14 {
15 ::EnterCriticalSection(&m_CS) ;
16 }
17
18 void UnLock()
19 {
20 ::LeaveCriticalSection(&m_CS) ;
21 }
22 } ;
23
24
25 /// 自动锁
26 /*自动锁原理:利用类的构造函数,自动加锁,析构函数自动解锁*/
27 class ZAutoLock
28 {
29 ZLock* m_pLock ;
30 public:
31 ZAutoLock(ZLock* pLock)
32 {
33 m_pLock = pLock ;
34 m_pLock->Lock() ;
35 }
36
37 ~ZAutoLock()
38 {
39 m_pLock->UnLock() ;
40 }
41 };