-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathBuilding H2O.cpp
36 lines (32 loc) · 872 Bytes
/
Building H2O.cpp
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
#include <semaphore.h>
class H2O {
public:
H2O() {
sem_init(&h_sem, 0, 1);
sem_init(&o_sem, 0, 0);
}
void hydrogen(function<void()> releaseHydrogen) {
sem_wait(&h_sem);
// Lock this - '++' is a critical code
pthread_mutex_lock(&mtx);
h++;
pthread_mutex_unlock(&mtx);
// releaseHydrogen() outputs "H". Do not change or remove this line.
releaseHydrogen();
if (h % 2 == 0)
sem_post(&o_sem);
else
sem_post(&h_sem);
}
void oxygen(function<void()> releaseOxygen) {
sem_wait(&o_sem);
// releaseOxygen() outputs "O". Do not change or remove this line.
releaseOxygen();
sem_post(&h_sem);
}
private:
sem_t h_sem;
sem_t o_sem;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
int h = 0;
};