-
Notifications
You must be signed in to change notification settings - Fork 10
/
printinor.cpp
39 lines (34 loc) · 864 Bytes
/
printinor.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
class Foo {
public:
int count = 0;
mutex mtx;
condition_variable cv;
Foo() {
count = 1;
//cv.notify_all();
}
void first(function<void()> printFirst) {
unique_lock<mutex> lck(mtx);
printFirst();
count = 2;
cv.notify_all();
}
void second(function<void()> printSecond) {
unique_lock<mutex> lck(mtx);
while(count != 2){
cv.wait(lck);
}
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
count = 3;
cv.notify_all();
}
void third(function<void()> printThird) {
unique_lock<mutex> lck(mtx);
while(count != 3){
cv.wait(lck);
}
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};