From c674f68b2e2cbe6e34453b67105e43a434952a32 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 19 Apr 2023 14:01:59 +0000 Subject: [PATCH] fix: check if all threads is stopped before destroye data --- data.c | 19 +++++++++++++++++-- philo.c | 26 ++++++++++++++++++-------- struct.h | 2 ++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/data.c b/data.c index 55c35c3..450ce7e 100644 --- a/data.c +++ b/data.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "./data.h" #include "philo.h" #include "philos.h" @@ -35,9 +36,23 @@ bool data_init(t_data *data) void data_destroyer(t_data *data) { - free(data->threads); - free(data->forks); + size_t i; + t_philo *philo; + bool stop; + + i = 0; + while (i < data->nb_philos) + { + philo = data->philos[i]; + pthread_mutex_lock(&philo->stop_mutex); + stop = philo->stop; + pthread_mutex_unlock(&philo->stop_mutex); + if (stop) + i++; + } pthread_mutex_destroy(&data->forks_mutex); pthread_mutex_destroy(&data->stop_mutex); + free(data->threads); + free(data->forks); philos_destroyer(data); } diff --git a/philo.c b/philo.c index 2e5d392..3049650 100644 --- a/philo.c +++ b/philo.c @@ -17,13 +17,19 @@ void philo_destroyer(t_philo *philo) free(philo); } -bool check(t_data *data) +bool check(t_philo *philo, t_data *data) { bool stop; pthread_mutex_lock(&data->stop_mutex); stop = data->stop; pthread_mutex_unlock(&data->stop_mutex); + if (stop) + { + pthread_mutex_lock(&philo->stop_mutex); + philo->stop = 1; + pthread_mutex_unlock(&philo->stop_mutex); + } return (stop); } @@ -51,11 +57,12 @@ bool philo_eat(t_philo *philo, t_data *data) right_fork = 0; while (left_fork == 0 || right_fork == 0) { - if (check(data)) + if (check(philo, data)) return (1); pthread_mutex_lock(&data->forks_mutex); left_fork = data->forks[philo->id]; - right_fork = data->forks[(philo->id + 1) % data->nb_philos]; + if (((philo->id + 1) % data->nb_philos) != philo->id) + right_fork = data->forks[(philo->id + 1) % data->nb_philos]; if (right_fork && left_fork) { data->forks[philo->id] = 0; @@ -85,18 +92,20 @@ void *philo_routine(void *arg) philo = arg; data = philo->data; - if (philo->id % 2) - usleep(data->sleep_time * 1000); + print_thinking(philo); + usleep(philo->id * (data->life_expectency / data->nb_philos)); while (true) { - print_thinking(philo); - if (check(data)) + if (check(philo, data)) return (NULL); if (philo_eat(philo, data)) return (NULL); - if (check(data)) + if (check(philo, data)) return (NULL); philo_sleep(data, philo); + if (check(philo, data)) + return (NULL); + print_thinking(philo); } return (NULL); } @@ -116,5 +125,6 @@ t_philo *philo_init(t_data *data) pthread_mutex_init(&philo->nb_meal_mutex, NULL); pthread_mutex_init(&philo->last_eat_mutex, NULL); pthread_mutex_init(&philo->last_sleep_mutex, NULL); + pthread_mutex_init(&philo->stop_mutex, NULL); return (philo); } diff --git a/struct.h b/struct.h index d42783f..c7bd438 100644 --- a/struct.h +++ b/struct.h @@ -28,6 +28,8 @@ typedef struct s_philo size_t last_eat; pthread_mutex_t last_sleep_mutex; size_t last_sleep; + pthread_mutex_t stop_mutex; + bool stop; t_data *data; } t_philo;