-
Notifications
You must be signed in to change notification settings - Fork 0
/
1117_H2O.cpp
71 lines (62 loc) · 1.88 KB
/
1117_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <condition_variable>
#include <functional>
#include <iostream>
#include <thread>
#include <vector>
class H2O {
private:
int mHcount;
int mOcount;
std::mutex mMutex;
std::condition_variable mConditionVariable;
public:
H2O() {
mHcount = 0;
mOcount = 0;
}
void hydrogen(std::function<void()> releaseHydrogen) {
std::unique_lock<std::mutex> lock(mMutex);
mConditionVariable.wait(lock, [=]() { return (mHcount != 2); });
// releaseHydrogen() outputs "H". Do not change or remove this line.
releaseHydrogen();
mHcount += 1;
if (mOcount == 1 && mHcount == 2) {
mOcount = 0;
mHcount = 0;
}
lock.unlock();
mConditionVariable.notify_all();
}
void oxygen(std::function<void()> releaseOxygen) {
std::unique_lock<std::mutex> lock(mMutex);
mConditionVariable.wait(lock, [=]() { return (mOcount != 1); });
// releaseOxygen() outputs "O". Do not change or remove this line.
releaseOxygen();
mOcount += 1;
if (mOcount == 1 && mHcount == 2) {
mOcount = 0;
mHcount = 0;
}
lock.unlock();
mConditionVariable.notify_all();
}
};
int main(int argc, char *argv[]) {
std::function<void()> printO = []() { std::cout << "O"; };
std::function<void()> printH = []() { std::cout << "H"; };
std::string input("OOHHHHHHHHOHHOO");
std::vector<std::thread> threads;
H2O h2O;
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] == 'H') {
threads.emplace_back(std::thread(&H2O::hydrogen, &h2O, printH));
}
if (input[i] == 'O') {
threads.emplace_back(std::thread(&H2O::oxygen, &h2O, printO));
}
}
for (auto iter = threads.begin(); iter < threads.end(); ++iter) {
iter->join();
}
return 0;
}