-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLock.cpp
52 lines (43 loc) · 777 Bytes
/
MyLock.cpp
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
/*********************************
*@fileName: MutexLock.cc
*@author : lemon
*@email : haohb13@gmail.com
*@created : 2015-07-02 11:02:13
**********************************/
#include "MyLock.h"
MutexLock::MutexLock()
: isLocking_(false)
{
pthread_mutex_init(&mutex_, NULL);
}
MutexLock::~MutexLock()
{
pthread_mutex_destroy(&mutex_);
}
void MutexLock::lock()
{
pthread_mutex_lock(&mutex_);
isLocking_ = true;
}
void MutexLock::unlock()
{
pthread_mutex_unlock(&mutex_);
isLocking_ = false;
}
bool MutexLock::isLocked() const
{
return isLocking_;
}
pthread_mutex_t * MutexLock::getMutexPtr()
{
return &mutex_;
}
MutexLockGuard::MutexLockGuard(MutexLock & mutex)
: mutex_(mutex)
{
mutex_.lock();
}
MutexLockGuard::~MutexLockGuard()
{
mutex_.unlock();
}