From eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 10 Jan 2021 10:54:04 +0100 Subject: Fixing bad performance at school by adding more delay to the death checking loop, Added mutex/semaphore to protect against eating and dying at the same time --- philo_one/src/routine.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'philo_one/src/routine.c') diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 340f3b7..01c74d5 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/09 14:32:33 by charles ### ########.fr */ +/* Updated: 2021/01/10 09:54:30 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,9 +67,14 @@ void *routine_philo(t_philo *arg) void *routine_death(t_philo *arg) { - while (!philo_finished(arg->conf) && - h_time_now() - arg->time_last_eat < arg->conf->timeout_death) - usleep(1000); + while (!philo_finished(arg->conf)) + { + pthread_mutex_lock(&arg->mutex_eat); + if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) + break ; + pthread_mutex_unlock(&arg->mutex_eat); + usleep(2000); + } event_die(arg); return (NULL); } -- cgit