dining-philosophers/include/philosopher.hpp

98 lines
2.6 KiB
C++
Raw Normal View History

2023-04-19 00:32:59 +02:00
#pragma once
#include <thread>
#include <vector>
#include <memory>
#include <mutex>
#include <iostream>
#include <semaphore>
#include <random.hpp>
#include <philosopher-state.hpp>
class Philosopher;
typedef std::shared_ptr<Philosopher> PhilosopherPtr;
typedef std::vector<PhilosopherPtr> ListOfPhilosophers;
typedef std::shared_ptr<ListOfPhilosophers> ListOfPhilosophersPtr;
extern std::mutex changingForkState;
extern std::mutex printing;
2023-04-19 01:28:50 +02:00
/**
* \brief A class representing a single philosopher having his dinner.
*
* Philosophers are seated in a round table.
* In order of being able to eat a philosopher should be able to take
2023-04-19 01:40:22 +02:00
* the two forks on his sides so no other of its neighbor philosophers
2023-04-19 01:28:50 +02:00
* should be eating at this time.
*/
2023-04-19 00:32:59 +02:00
class Philosopher {
private:
2023-04-19 01:28:50 +02:00
//! The position of the philosopher in the table.
2023-04-19 00:32:59 +02:00
int numberOfPhilosopher;
2023-04-19 01:28:50 +02:00
//! What the philosopher is doing.
2023-04-19 00:32:59 +02:00
PhilosopherState state{PhilosopherState::THINKING};
2023-04-19 01:28:50 +02:00
//! The vector of philosophers in the table.
2023-04-19 00:32:59 +02:00
ListOfPhilosophersPtr philosophers;
2023-04-19 01:28:50 +02:00
//! The function to set the state of the philosopher.
2023-04-19 00:32:59 +02:00
void
setState(PhilosopherState state);
2023-04-19 01:40:22 +02:00
//! Method to guess the numberOfPhilosopher of the left neighbor of this philosopher.
2023-04-19 00:32:59 +02:00
size_t
leftPhilosopherNumber(void);
2023-04-19 01:40:22 +02:00
//! Method to guess the numberOfPhilosopher of the right neighbor of this philosopher.
2023-04-19 00:32:59 +02:00
size_t
rightPhilosopherNumber(void);
2023-04-19 01:40:22 +02:00
//! Retrieve the left neighbor.
2023-04-19 00:32:59 +02:00
PhilosopherPtr
leftPhilosopher(void);
2023-04-19 01:40:22 +02:00
//! Retrieve the right neighbor.
2023-04-19 00:32:59 +02:00
PhilosopherPtr
rightPhilosopher(void);
2023-04-19 01:28:50 +02:00
//! Makes the philosopher think for a while.
2023-04-19 00:32:59 +02:00
void
2023-04-19 01:28:50 +02:00
think(void);
2023-04-19 00:32:59 +02:00
2023-04-19 01:40:22 +02:00
//! The philosopher waits until the forks of his neighbors are free and then takes the two forks.
2023-04-19 00:32:59 +02:00
void
2023-04-19 01:28:50 +02:00
takeForks(void);
2023-04-19 00:32:59 +02:00
2023-04-19 01:28:50 +02:00
//! The philosopher eats the spaghetti with the two forks.
2023-04-19 00:32:59 +02:00
void
2023-04-19 01:28:50 +02:00
eat(void);
2023-04-19 00:32:59 +02:00
2023-04-19 01:40:22 +02:00
//! The philosopher puts the forks in the table to be used by his neighbors.
2023-04-19 00:32:59 +02:00
void
2023-04-19 01:28:50 +02:00
putForks(void);
2023-04-19 00:32:59 +02:00
public:
2023-04-19 01:28:50 +02:00
//! Retrieves the philosopher state.
2023-04-19 00:32:59 +02:00
PhilosopherState
2023-04-19 01:28:50 +02:00
getState(void);
/**
* \brief Philosopher constructor
* @param philosophers The vector of in the table philosophers.
* @param numberOfPhilosopher The position of this philosopher in the table.
*/
2023-04-19 00:32:59 +02:00
Philosopher(ListOfPhilosophersPtr philosophers, int numberOfPhilosopher);
2023-04-19 01:28:50 +02:00
//! Whenever this philosopher was the two forks currently.
2023-04-19 00:32:59 +02:00
std::binary_semaphore hasBothForks{0};
2023-04-19 01:28:50 +02:00
//! Starts the philosopher dinner.
void startThread(void);
//! Test if the philosopher should be able to take both forks currently.
void test(void);
2023-04-19 00:32:59 +02:00
};