-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiningPhilosophers.cpp
139 lines (109 loc) · 2.96 KB
/
DiningPhilosophers.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
#include <iostream>
#include <thread>
#include <ctime>
#include <chrono>
typedef struct
{
pthread_mutex_t lock;
pthread_cond_t wait;
int value;
int waiters;
} sema;
sema *InitSem(int count)
{
sema *s;
s = (sema *)malloc(sizeof(sema));
if(s == NULL) {
return(NULL);
}
s->value = count;
s->waiters = 0;
pthread_cond_init(&(s->wait),NULL);
pthread_mutex_init(&(s->lock),NULL);
return(s);
}
void P(sema *s)
{
pthread_mutex_lock(&(s->lock));
if(s->value>0)
s->value--;
while(s->value < 0) {
if(s->waiters < (-1 * s->value)) {
s->waiters++;
pthread_cond_wait(&(s->wait),&(s->lock));
s->waiters--;
} else {
break;
}
}
pthread_mutex_unlock(&(s->lock));
return;
}
void V(sema *s)
{
pthread_mutex_lock(&(s->lock));
if(s->value==0)
s->value++;
if(s->value <= 0)
{
pthread_cond_signal(&(s->wait));
}
pthread_mutex_unlock(&(s->lock));
}
bool try_lock(sema *s){
bool ch =false;
pthread_mutex_lock(&(s->lock));
if(s->value>0) ch = true;
pthread_mutex_unlock(&(s->lock));
return ch;
}
constexpr int num_Phil = 5;
std::thread philosphers[num_Phil];
sema mtx[num_Phil];
sema cout_mutex;
int ate[5] = {0};
int intrupt = 100;
void print(std::string str){
P(&cout_mutex);
std::cout << str << std::endl;
V(&cout_mutex);
}
void think(int id){
std::this_thread::sleep_for(std::chrono::milliseconds(500));
print("Philosopher " + std::to_string(id) + " is thinking.");
}
bool eat(int id, int left, int right) {
while(1) if (try_lock(&mtx[left])) {
print("Philosopher " + std::to_string(id) + " got the fork " + std::to_string(left));
if (try_lock(&mtx[right])) {
print("Philosopher " + std::to_string(id) + " got the fork " + std::to_string(right) +
"\nPhilosopher " + std::to_string(id) + " eats."); // print
return true;
} else {
V(&mtx[left]);
}
}
return false;
}
void putDownForks(int left, int right) {
V(&mtx[left]);
V(&mtx[right]);
}
void dinner_started(int philID) {
int lIndex = std::min(philID, (philID + 1) % (num_Phil));
int rIndex = std::max(philID, (philID + 1) % (num_Phil));
while (intrupt-- > 0) {
if (eat(philID, lIndex, rIndex)) {
ate[philID]++;
std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
}
}
void dine(){
for (int i = 0; i < num_Phil; ++i) philosphers[i] = std::thread(dinner_started, i);
for (int i = 0; i < num_Phil; ++i) philosphers[i].join();
}
int main() {
dine();
for (int i = 0; i < num_Phil; ++i) std::cout << i << " = " << ate[i] << std::endl;
}