-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhilosopher.cpp
More file actions
47 lines (39 loc) · 1.76 KB
/
Philosopher.cpp
File metadata and controls
47 lines (39 loc) · 1.76 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
/**
*Represents a philosopher in the Dining Philosophers problem.
* Each philosopher has an ID, a left fork, and a right fork.
* The `eat` method simulates the philosopher eating for a certain duration.
* The `think` method simulates the philosopher thinking for a certain duration.
* The `dine` method represents the process of the philosopher eating and thinking in a loop.
* The philosopher thinks, picks up the left and right fork, eats, and then puts down the forks.
* This process repeats for a specified number of iterations.
**/
#include "Philosopher.h"
#include <iostream>
#include <chrono>
#include <thread>
Philosopher::Philosopher(int id, Fork* left_fork, Fork* right_fork) : id(id), left_fork(left_fork), right_fork(right_fork) {}
void Philosopher::eat() {
std::cout << "Philosopher " << id << " is eating." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Simulating eating
}
void Philosopher::think() {
std::cout << "Philosopher " << id << " is thinking." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Simulating thinking
}
void Philosopher::dine() {
// Each philosopher will eat 3 times
for (int i = 0; i < 3; i++) {
think();
// Pick up
left_fork->pick_up();
std::cout << "Philosopher " << id << " picked up left fork." << std::endl;
right_fork->pick_up();
std::cout << "Philosopher " << id << " picked up right fork." << std::endl;
eat();
// Put down
right_fork->put_down();
std::cout << "Philosopher " << id << " put down right fork." << std::endl;
left_fork->put_down();
std::cout << "Philosopher " << id << " put down left fork." << std::endl;
}
}