-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1117. Building H2O.cpp
45 lines (40 loc) · 1.08 KB
/
1117. 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
37
38
39
40
41
42
43
44
45
class H2O {
public:
mutex m;
// guarantee "HHO" by thread arrival order, not required to pass the oj
queue<pair<condition_variable*, function<void()>>> hq, oq;
H2O() {
}
void Pass(unique_lock<mutex>* lk) {
auto [cvh1, f1] = hq.front(); hq.pop();
invoke(f1);
auto [cvh2, f2] = hq.front(); hq.pop();
invoke(f2);
auto [cvo, f3] = oq.front(); oq.pop();
invoke(f3);
lk->unlock();
cvh1->notify_one();
cvh2->notify_one();
cvo->notify_one();
}
void hydrogen(function<void()> releaseHydrogen) {
unique_lock lk(m);
condition_variable cv;
hq.push({&cv, releaseHydrogen});
if (hq.size()>=2 && oq.size()>=1) {
Pass(&lk);
return;
}
cv.wait(lk);
}
void oxygen(function<void()> releaseOxygen) {
unique_lock lk(m);
condition_variable cv;
oq.push({&cv, releaseOxygen});
if (hq.size()>=2 && oq.size()>=1) {
Pass(&lk);
return;
}
cv.wait(lk);
}
};