-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.h
More file actions
48 lines (44 loc) · 973 Bytes
/
Copy pathmutex.h
File metadata and controls
48 lines (44 loc) · 973 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
/*************************************************************************
> File Name: mutex.cpp
> Author:jieni
> Mail:
> Created Time: 2020年07月24日 星期五 16时44分27秒
************************************************************************/
#include <pthread.h>
class MutexLock {
public:
MutexLock() {pthread_mutex_init(&mutex_, NULL); }
MutexLock(const MutexLock& mutex)
{
mutex_ = mutex.mutex_;
}
~MutexLock() {pthread_mutex_destroy(&mutex_); }
void lock()
{
pthread_mutex_lock(&mutex_);
}
void unlock()
{
pthread_mutex_unlock(&mutex_);
}
pthread_mutex_t *get_mutex()
{
return &mutex_;
}
private:
pthread_mutex_t mutex_;
};
class MutexLockGuard {
public:
explicit MutexLockGuard(MutexLock& mutex)
: mutex_(mutex)
{
mutex_.lock();
}
~MutexLockGuard()
{
mutex_.unlock();
}
private:
MutexLock& mutex_;
};