-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.h
59 lines (48 loc) · 1.11 KB
/
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
52
53
54
55
56
57
58
59
#ifndef MUTEX_H_
#define MUTEX_H_
#ifdef PTHREAD
#include<pthread.h>
namespace Mutex{
class MutexLock{
public:
MutexLock();
~MutexLock();
void Lock();
void UnLock();
pthread_mutex_t* GetPthreadMutex();
private:
MutexLock(const MutexLock&); //for not copyable
MutexLock& operator=(const MutexLock&); //for not copyable
pthread_mutex_t mutex_;
};
class MutexLockGuard{
public:
explicit MutexLockGuard(MutexLock& mutex_lock):
mutex_lock_(mutex_lock){
mutex_lock_.Lock();
}
~MutexLockGuard(){
mutex_lock_.UnLock();
}
private:
MutexLockGuard(const MutexLockGuard&); //for noncopyable
MutexLockGuard& operator = (const MutexLockGuard&); //for noncopyable
MutexLock& mutex_lock_;
};
class Condition{
public:
explicit Condition(MutexLock&);
~Condition();
void Wait();
int WaitTimeOut(int time); //in second
void Notify();
void NotifyAll();
private:
Condition(const Condition&);
Condition& operator= (const Condition&);
MutexLock& mutex_lock_;
pthread_cond_t pcond_;
};
};
#endif
#endif