diff --git a/Print FooBar Alternately b/Print FooBar Alternately new file mode 100644 index 0000000..af087fd --- /dev/null +++ b/Print FooBar Alternately @@ -0,0 +1,30 @@ +class FooBar { +private: + int n; + mutex m1,m2; +public: + FooBar(int n) { + m2.lock(); + this->n = n; + } + + void foo(function printFoo) { + + for (int i = 0; i < n; i++) { + m1.lock(); + // printFoo() outputs "foo". Do not change or remove this line. + printFoo(); + m2.unlock(); + } + } + + void bar(function printBar) { + + for (int i = 0; i < n; i++) { + m2.lock(); + // printBar() outputs "bar". Do not change or remove this line. + printBar(); + m1.unlock(); + } + } +};