forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1114. Print in Order.cpp
75 lines (63 loc) · 2.05 KB
/
1114. Print in Order.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
72
73
74
75
//Runtime: 1624 ms, faster than 10.44% of C++ online submissions for Print in Order.
//Memory Usage: 9.2 MB, less than 100.00% of C++ online submissions for Print in Order.
class Foo {
public:
int count;
Foo() {
count = 0;
}
void first(function<void()> printFirst) {
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
count += 1;
}
void second(function<void()> printSecond) {
while(count < 1){
}
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
count += 1;
}
void third(function<void()> printThird) {
while(count < 2){
}
// printThird() outputs "third". Do not change or remove this line.
printThird();
count += 1;
}
};
//https://leetcode.com/problems/print-in-order/discuss/343384/C%2B%2BWhy-most-of-the-solutions-using-mutex-are-wrong%2Bsolution
//Runtime: 152 ms, faster than 63.21% of C++ online submissions for Print in Order.
//Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Print in Order.
class Foo {
public:
int count;
mutex mtx;
condition_variable cv;
Foo() {
count = 1;
}
void first(function<void()> printFirst) {
unique_lock<mutex> lck(mtx);
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
count += 1;
cv.notify_all();
}
void second(function<void()> printSecond) {
unique_lock<mutex> lck(mtx);
// printSecond() outputs "second". Do not change or remove this line.
cv.wait(lck, [this](){return count == 2;});
printSecond();
count += 1;
cv.notify_all();
}
void third(function<void()> printThird) {
unique_lock<mutex> lck(mtx);
// printThird() outputs "third". Do not change or remove this line.
cv.wait(lck, [this](){return count == 3;});
printThird();
count += 1;
cv.notify_all();
}
};