-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCLock.h
More file actions
65 lines (52 loc) · 854 Bytes
/
Copy pathCLock.h
File metadata and controls
65 lines (52 loc) · 854 Bytes
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# ifndef _BASELOCK_H_
# define _BASELOCK_H_
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __linux__
#include <pthread.h>
#endif
#include <iostream>
using namespace std;
//各种类型的锁的基类
class BaseLock
{
public:
BaseLock(){}
virtual ~BaseLock(){}
virtual void lock() = 0 ;
virtual void unlock() = 0 ;
};
//互斥锁继承基类
class Mutex :public BaseLock
{
public:
Mutex();
~Mutex();
virtual void lock() ;
virtual void unlock() ;
private:
#if defined _WIN32
HANDLE m_hMutex;
#endif
#ifdef __linux__
pthread_mutex_t m_hMutex;
#endif
};
class CLock
{
public:
CLock( BaseLock *baseLock):m_cBaseLock(baseLock){
//构造函数里通过基类锁调用加锁函数(多态)
m_cBaseLock->lock();
}
~CLock(){
//析构函数先解锁
m_cBaseLock->unlock();
}
private:
//常引用变量,需要在初始化列表初始
//多态机制
BaseLock* m_cBaseLock;
};
#endif