-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemaphore.h
61 lines (44 loc) · 1.25 KB
/
Semaphore.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
#ifndef SEMPHORE_H
#define SEMPHORE_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// Logic based around: http://www.greenteapress.com/semaphores/
// It's a free book: read it now!
using namespace std;
class Semaphore {
public:
Semaphore(int value) {
_value = value;
pthread_mutex_init(&_mutex, NULL);
pthread_cond_init(&_cond, NULL);
}
void wait() {
pthread_mutex_lock(&_mutex);
--_value;
if(_value < 0) {
// Rather than release all threads, it keeps blocking
// all but one thread, hence the do... while.
do {
pthread_cond_wait (&_cond, &_mutex);
} while(_wakeups < 1);
--_wakeups;
}
pthread_mutex_unlock(&_mutex);
}
void signal() {
pthread_mutex_lock(&_mutex);
++_value;
if(_value <= 0) {
++_wakeups;
pthread_cond_signal(&_cond);
}
pthread_mutex_unlock(&_mutex);
}
private:
int _value;
int _wakeups;
pthread_mutex_t _mutex;
pthread_cond_t _cond;
};
#endif /* SEMPHORE_H */