-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC1115.cpp
162 lines (135 loc) · 3.46 KB
/
LC1115.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
1115. Print FooBar Alternately
Suppose you are given the following code:
class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print("foo");
}
}
public void bar() {
for (int i = 0; i < n; i++) {
print("bar");
}
}
}
The same instance of FooBar will be passed to two different threads.
Thread A will call foo() while thread B will call bar(). Modify the given program to output "foobar" n times.
Example 1:
Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously.
One of them calls foo(), while the other calls bar(). "foobar" is being output 1 time.
Example 2:
Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times.
*/
//Approach 1: using 'semaphore'
#include <semaphore.h>
class FooBar
{
private:
int n;
sem_t foo_sem;
sem_t bar_sem;
public:
FooBar(int n)
{
this->n = n;
sem_init(&foo_sem, 0, 1);
sem_init(&bar_sem, 0, 0);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
~FooBar()
{
sem_destroy(&foo_sem);
sem_destroy(&bar_sem);
}
void foo(function<void()> printFoo)
{
for (int i = 0; i < n; i++)
{
sem_wait(&foo_sem);
// printFoo() outputs "foo". Do not change or remove this line.
printFoo();
sem_post(&bar_sem);
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
sem_wait(&bar_sem);
// printBar() outputs "bar". Do not change or remove this line.
printBar();
sem_post(&foo_sem);
}
}
};
//Approach 2: using 'mutex and condition variable'
class FooBar
{
private:
int n;
std::condition_variable barrier;
std::mutex mutex;
bool flag = {true};
public:
FooBar(int n) {
this->n = n;
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; i++) {
std::unique_lock lock(mutex);
barrier.wait(lock, [&](){return flag; });
// printFoo() outputs "foo". Do not change or remove this line.
printFoo();
flag = false;
barrier.notify_one();
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
std::unique_lock lock(mutex);
barrier.wait(lock, [&](){return !flag; });
// printBar() outputs "bar". Do not change or remove this line.
printBar();
flag = true;
barrier.notify_one();
}
}
};
//Approach 3: using 'volatile'
class FooBar
{
private:
int n;
volatile int barrier = {0};
public:
FooBar(int n) {
this->n = n;
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; i++) {
while (barrier != 0)
{
std::this_thread::yield();
}
// printFoo() outputs "foo". Do not change or remove this line.
printFoo();
++barrier;
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
while(barrier != 1)
{
std::this_thread::yield();
}
// printBar() outputs "bar". Do not change or remove this line.
printBar();
--barrier;
}
}
};