fix: stop when nb_meal is reached

This commit is contained in:
Camille Chauvet 2023-04-25 14:50:01 +00:00
parent 891431c1be
commit 02a80af93b
4 changed files with 8 additions and 5 deletions

2
data.h
View File

@ -9,7 +9,7 @@ typedef struct s_data
size_t sleep_time; size_t sleep_time;
size_t life_expectency; size_t life_expectency;
size_t nb_philos; size_t nb_philos;
ssize_t nb_meals; size_t nb_meals;
void **philos; void **philos;
pthread_t *threads; pthread_t *threads;
pthread_mutex_t *forks; pthread_mutex_t *forks;

4
main.c
View File

@ -65,12 +65,12 @@ void *check_routine(t_data *data)
print_died(philo); print_died(philo);
return (NULL); return (NULL);
} }
i++; if (get_min_meal(data) >= data->nb_meals)
if ((ssize_t) get_min_meal(data) == data->nb_meals)
{ {
stop(data); stop(data);
return (NULL); return (NULL);
} }
i++;
} }
} }
} }

View File

@ -63,6 +63,9 @@ bool philo_eat(t_philo *philo, t_data *data)
pthread_mutex_lock(&philo->last_eat_mutex); pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time(); philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex); pthread_mutex_unlock(&philo->last_eat_mutex);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */ /* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */
/* Updated: 2023/04/25 14:05:15 by cchauvet ### ########.fr */ /* Updated: 2023/04/25 16:41:14 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@
typedef struct s_philo typedef struct s_philo
{ {
size_t id; size_t id;
pthread_mutex_t nb_meal_mutex; pthread_mutex_t nb_meal_mutex;
size_t nb_meal; size_t nb_meal;
pthread_mutex_t last_eat_mutex; pthread_mutex_t last_eat_mutex;