-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBarberShop.cpp
81 lines (71 loc) · 1.89 KB
/
BarberShop.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
// Problem: https://www.educative.io/blog/top-five-concurrency-interview-questions-for-software-engineers
#include <iostream>
#include <condition_variable>
#include <mutex>
using namespace std;
class Semaphore {
public:
Semaphore(int count): count_(count) {}
void acquire() {
unique_lock<mutex> lock(mtx_);
cond_.wait(lock, [this] {
return count_ > 0;
});
--count_;
}
void release() {
unique_lock<mutex> lock(mtx_);
++count_;
}
private:
int count_;
mutex mtx_;
condition_variable cond_;
};
class BarberShop {
public:
BarberShop(int num_chairs): empty_chairs_(num_chairs),
is_barber_asleep_(false),
customer_waiting_(0) {}
void barberThreadRun() {
while (true) {
{
unique_lock<mutex> lock(mtx_);
barber_asleep_.wait(lock, [this] {
return num_customers_ > 0;
});
is_barber_asleep_ = false;
}
// No-Op. giveHairCut();
customer_waiting_.release();
}
}
void customerEnters() {
{
unique_lock<mutex> lock(mtx_);
if (empty_chairs_ == 0) return;
// (empty_chairs_ > 0) {
++num_customers_;
--empty_chairs_;
if (is_barber_asleep_) {
barber_asleep_.notify_one();
}
// Customer waits on a chair.
customer_waiting_.acquire();
}
customerLeaves();
}
void customerLeaves() {
unique_lock<mutex> lock(mtx_);
--num_customers_;
}
private:
mutex mtx_;
// Indicates the number of chairs that are free.
int empty_chairs_;
// Indicates the number of customers to be served.
int num_customers_;
Semaphore customer_waiting_;
bool is_barber_asleep_;
condition_variable barber_asleep_;
};