Skip to content

Commit 77c53e9

Browse files
authored
Create 1114-print-in-order.cpp
1 parent 4196f62 commit 77c53e9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1114-print-in-order.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Foo {
2+
public:
3+
int count = 0;
4+
mutex mtx;
5+
condition_variable cv;
6+
Foo() {
7+
count = 1;
8+
}
9+
10+
void first(function<void()> printFirst) {
11+
unique_lock<mutex> lck(mtx);
12+
// printFirst() outputs "first". Do not change or remove this line.
13+
printFirst();
14+
count = 2;
15+
cv.notify_all();
16+
}
17+
18+
void second(function<void()> printSecond) {
19+
unique_lock<mutex> lck(mtx);
20+
cv.wait(lck, [this]() { return count == 2;});
21+
// printSecond() outputs "second". Do not change or remove this line.
22+
printSecond();
23+
count = 3;
24+
cv.notify_all();
25+
}
26+
27+
void third(function<void()> printThird) {
28+
unique_lock<mutex> lck(mtx);
29+
cv.wait(lck, [this]() { return count == 3;});
30+
// printThird() outputs "third". Do not change or remove this line.
31+
printThird();
32+
}
33+
};

0 commit comments

Comments
 (0)