Skip to content

Commit 497ec96

Browse files
authored
Update print-in-order.cpp
1 parent 2a5e1fd commit 497ec96

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

C++/print-in-order.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@ class Foo {
66
Foo() {
77
}
88

9+
void first(function<void()> printFirst) {
10+
// printFirst() outputs "first". Do not change or remove this line.
11+
printFirst();
12+
p1_.set_value();
13+
}
14+
15+
void second(function<void()> printSecond) {
16+
p1_.get_future().wait();
17+
// printSecond() outputs "second". Do not change or remove this line.
18+
printSecond();
19+
p2_.set_value();
20+
}
21+
22+
void third(function<void()> printThird) {
23+
p2_.get_future().wait();
24+
// printThird() outputs "third". Do not change or remove this line.
25+
printThird();
26+
}
27+
28+
private:
29+
promise<void> p1_;
30+
promise<void> p2_;
31+
};
32+
33+
// Time: O(n)
34+
// Space: O(1)
35+
class Foo2 {
36+
public:
37+
Foo2() {
38+
}
39+
940
void first(function<void()> printFirst) {
1041
{
1142
unique_lock<mutex> l(m_);
@@ -46,9 +77,9 @@ class Foo {
4677

4778
// Time: O(n)
4879
// Space: O(1)
49-
class Foo2 {
80+
class Foo3 {
5081
public:
51-
Foo2() {
82+
Foo3() {
5283
m1_.lock();
5384
m2_.lock();
5485
}

0 commit comments

Comments
 (0)