-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFooBar.cpp
More file actions
47 lines (38 loc) · 886 Bytes
/
FooBar.cpp
File metadata and controls
47 lines (38 loc) · 886 Bytes
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
40
41
42
43
44
45
46
47
#include<iostream>
#include<semaphore.h>
using namespace std;
class FooBar {
private:
int n;
sem_t sem_foo, sem_bar;
public:
FooBar(int n) {
this->n = n;
sem_init(&sem_foo, 0, 0);
sem_init(&sem_bar, 0, 1);
}
~FooBar()
{
sem_destroy(&sem_foo);
sem_destroy(&sem_bar);
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; i++) {
sem_wait(&sem_bar);
// printFoo() outputs "foo". Do not change or remove this line.
printFoo();
sem_post(&sem_foo);
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
sem_wait(&sem_foo);
// printBar() outputs "bar". Do not change or remove this line.
printBar();
sem_post(&sem_bar);
}
}
};
int main()
{
}