-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdining_philosophers_problem.cpp
More file actions
126 lines (110 loc) · 4.06 KB
/
Copy pathdining_philosophers_problem.cpp
File metadata and controls
126 lines (110 loc) · 4.06 KB
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
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <mutex>
#include <random>
#include <string>
#include <thread>
std::mutex cout_mutex;
struct Fork {
std::mutex mutex;
};
struct Dinner {
std::atomic<bool> ready {false};
// Make forks adjustable
std::array<Fork, 6> forks;
~Dinner() { std::cout << "Dinner is over"; }
};
class Philosopher {
std::mt19937 rng{ std::random_device {}() };
const std::string name;
const Dinner& dinner;
Fork& left;
Fork& right;
std::thread worker;
void live();
void dine();
void ponder();
public:
Philosopher(std::string name_, const Dinner& dinn, Fork& l, Fork& r)
: name(std::move(name_)), dinner(dinn), left(l), right(r), worker(&Philosopher::live, this) {}
~Philosopher() {
worker.join();
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << name << " went to sleep." << std::endl;
}
};
void Philosopher::live() {
while(not dinner.ready);
do {
std::lock(left.mutex, right.mutex);
dine();
if(not dinner.ready) break;
ponder();
} while(dinner.ready);
}
void Philosopher::dine() {
std::lock_guard<std::mutex> left_lock(left.mutex, std::adopt_lock);
std::lock_guard<std::mutex> right_lock(right.mutex, std::adopt_lock);
thread_local std::array<const char*, 3> foods {{"chicken", "rice", "soda"}};
thread_local std::array<const char*, 3> reactions {{
"I like %s!", "This %s is good.", "Mmm, %s..."
}};
thread_local std::uniform_int_distribution<> dist(1, 6);
std::shuffle(foods.begin(), foods.end(), rng);
std::shuffle(reactions.begin(), reactions.end(), rng);
if(not dinner.ready) return;
{
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout<< name << "started eating." << std::endl;
}
constexpr size_t buf_size = 64;
char buffer[buf_size];
for(int i=0; i < 3; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(dist(rng)*50));
snprintf(buffer, buf_size, reactions[i], foods[i]);
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << name << ": " << buffer << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(dist(rng))*50);
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << name << " finished and left." << std::endl;
}
void Philosopher::ponder() {
static constexpr std::array<const char*, 5> topics {{
"politics", "art", "meaning of life", "source of morality", "how many straws make a bale"
}};
thread_local std::uniform_int_distribution<> wait(1, 6);
thread_local std::uniform_int_distribution<> dist(0, topics.size() - 1);
while(dist(rng) > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(wait(rng)*150));
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << name << " is pondering about " << topics[dist(rng)] << '.' << std::endl;
if(not dinner.ready) return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(wait(rng)*150));
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << name << " is hungry again!" << std::endl;
}
int main() {
Dinner dinner;
// Adjustable number of philosophers
std::array<Philosopher, 6> philosophers {{
{"Aristotle", dinner, dinner.forks[0], dinner.forks[1]},
{"Kant", dinner, dinner.forks[1], dinner.forks[2]},
{"Spinoza", dinner, dinner.forks[2], dinner.forks[3]},
{"Marx", dinner, dinner.forks[3], dinner.forks[4]},
{"Russell", dinner, dinner.forks[4], dinner.forks[0]},
{"Nasser", dinner, dinner.forks[4], dinner.forks[0]}
}};
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Dinner started!" << std::endl;
dinner.ready = true;
std::this_thread::sleep_for(std::chrono::seconds(5));
dinner.ready = false;
std::lock_guard<std::mutex> cout_lock(cout_mutex);
std::cout << "It is dark outside..." << std::endl;
}