forked from voidccc/mini-muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutex.h
51 lines (46 loc) · 892 Bytes
/
Mutex.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
//author voidccc
#ifndef MUTEX_H
#define MUTEX_H
#include <pthread.h>
class MutexLock
{
public:
MutexLock()
{
pthread_mutex_init(&_mutexid, NULL);
}
~MutexLock()
{
pthread_mutex_destroy(&_mutexid);
}
void lock()
{
pthread_mutex_lock(&_mutexid);
}
void unlock()
{
pthread_mutex_unlock(&_mutexid);
}
pthread_mutex_t* getPthreadMutex()
{
return &_mutexid;
}
private:
pthread_mutex_t _mutexid;
};
class MutexLockGuard
{
public:
MutexLockGuard(MutexLock& mutex)
:_mutex(mutex)
{
_mutex.lock();
}
~MutexLockGuard()
{
_mutex.unlock();
}
private:
MutexLock& _mutex;
};
#endif