-
Notifications
You must be signed in to change notification settings - Fork 1
/
MutexLock.h
60 lines (49 loc) · 847 Bytes
/
MutexLock.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
52
53
54
55
56
57
58
59
60
/*
* MutexLock.h
*
* Created on: 2014-10-23
* Author: hgw
*/
#ifndef MUTEXLOCK_H_
#define MUTEXLOCK_H_
#include <pthread.h>
class MutexLock
{
public:
MutexLock() { pthread_mutex_init(&mutex_, NULL);}
~MutexLock() { pthread_mutex_destroy(&mutex_); }
void lock()
{
pthread_mutex_lock(&mutex_);
}
void unlock()
{
pthread_mutex_unlock(&mutex_);
}
private:
pthread_mutex_t mutex_;
//noncopyable
private:
MutexLock( const MutexLock& );
MutexLock& operator=( const MutexLock& );
};
class MutexLockGuard
{
public:
explicit MutexLockGuard(MutexLock& mutex)
: mutex_(mutex)
{
mutex_.lock();
}
~MutexLockGuard()
{
mutex_.unlock();
}
private:
MutexLock& mutex_;
//noncopyable
private:
MutexLockGuard( const MutexLockGuard& );
MutexLockGuard& operator=( const MutexLockGuard& );
};
#endif /* MUTEXLOCK_H_ */