Skip to content

Commit bbc01b5

Browse files
authored
Create solution.cpp
1 parent 9a7e491 commit bbc01b5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1114.Print_In_Order/solution.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Foo
2+
{
3+
public:
4+
Foo()
5+
:one_(false),
6+
second_(false)
7+
{
8+
9+
}
10+
11+
void first(function<void()> printFirst)
12+
{
13+
unique_lock<mutex> lock(mutex_);
14+
printFirst();
15+
one_ = true;
16+
condition_.notify_all();
17+
}
18+
19+
void second(function<void()> printSecond)
20+
{
21+
unique_lock<mutex> lock(mutex_);
22+
condition_.wait(lock, [this]() { return one_; });
23+
printSecond();
24+
second_ = true;
25+
condition_.notify_all();
26+
}
27+
28+
void third(function<void()> printThird)
29+
{
30+
unique_lock<mutex> lock(mutex_);
31+
condition_.wait(lock, [this]() { return second_; });
32+
printThird();
33+
}
34+
private:
35+
condition_variable condition_;
36+
mutex mutex_;
37+
bool one_;
38+
bool second_;
39+
};

0 commit comments

Comments
 (0)