From 02a80af93b42f0c1a503f22a742f86ee7e2bfefa Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 25 Apr 2023 14:50:01 +0000 Subject: [PATCH] fix: stop when nb_meal is reached --- data.h | 2 +- main.c | 4 ++-- philo.c | 3 +++ philo.h | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/data.h b/data.h index ffd53be..3648656 100644 --- a/data.h +++ b/data.h @@ -9,7 +9,7 @@ typedef struct s_data size_t sleep_time; size_t life_expectency; size_t nb_philos; - ssize_t nb_meals; + size_t nb_meals; void **philos; pthread_t *threads; pthread_mutex_t *forks; diff --git a/main.c b/main.c index 2ba46b8..d49a2f4 100644 --- a/main.c +++ b/main.c @@ -65,12 +65,12 @@ void *check_routine(t_data *data) print_died(philo); return (NULL); } - i++; - if ((ssize_t) get_min_meal(data) == data->nb_meals) + if (get_min_meal(data) >= data->nb_meals) { stop(data); return (NULL); } + i++; } } } diff --git a/philo.c b/philo.c index 3f5f2f0..44a24de 100644 --- a/philo.c +++ b/philo.c @@ -63,6 +63,9 @@ bool philo_eat(t_philo *philo, t_data *data) pthread_mutex_lock(&philo->last_eat_mutex); philo->last_eat = get_time(); 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); } diff --git a/philo.h b/philo.h index 78c0630..b0acc35 100644 --- a/philo.h +++ b/philo.h @@ -6,7 +6,7 @@ /* By: cchauvet