Skip to content

Commit 2a97feb

Browse files
authored
Merge pull request #3 from aryaman0406/aryaman0406-patch-3
Create FooBar class for alternating output
2 parents 3de0189 + 9f01372 commit 2a97feb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Print FooBar Alternately

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class FooBar {
2+
private:
3+
int n;
4+
mutex m1,m2;
5+
public:
6+
FooBar(int n) {
7+
m2.lock();
8+
this->n = n;
9+
}
10+
11+
void foo(function<void()> printFoo) {
12+
13+
for (int i = 0; i < n; i++) {
14+
m1.lock();
15+
// printFoo() outputs "foo". Do not change or remove this line.
16+
printFoo();
17+
m2.unlock();
18+
}
19+
}
20+
21+
void bar(function<void()> printBar) {
22+
23+
for (int i = 0; i < n; i++) {
24+
m2.lock();
25+
// printBar() outputs "bar". Do not change or remove this line.
26+
printBar();
27+
m1.unlock();
28+
}
29+
}
30+
};

0 commit comments

Comments
 (0)