From 82c1d6bc9558b2845c8e5e7e1561209b8b56f31d Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 15:00:56 +0200 Subject: [PATCH] Change the order of waiting for term and checking This is done so that a philosopher truly cannot write to terminal after it dies. Before this commit, a philosopher that is alive would wait for term semaphore. Meanwhile something could change his state to not alive but he could then get the semaphore and print anyway. I thought about having the condition inside as well as on the outside of the sem_wait. But having it just after the sem_wait should be alright. --- philo_bonus/helpers.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/philo_bonus/helpers.c b/philo_bonus/helpers.c index c7e54b0..44c7980 100644 --- a/philo_bonus/helpers.c +++ b/philo_bonus/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 15:32:49 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 14:45:17 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,12 +43,10 @@ void print_timestamp(t_philo *philo, const char *str) void report(t_philo *philo, const char *str) { + sem_wait(philo->semaphores.term); if (is_alive(philo)) - { - sem_wait(philo->semaphores.term); print_timestamp(philo, str); - sem_post(philo->semaphores.term); - } + sem_post(philo->semaphores.term); return ; } -- 2.30.2