From ecc704da7f56b33cb46a2b1569ddbfa637ce5d54 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 13 Aug 2024 12:25:24 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20print?= =?UTF-8?q?=20after=20death?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/log.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/philo/log.c b/philo/log.c index 6214505..04ef137 100644 --- a/philo/log.c +++ b/philo/log.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ -/* Updated: 2024/08/13 10:22:30 by adjoly ### ########.fr */ +/* Updated: 2024/08/13 12:21:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,20 +37,24 @@ void log_philo(t_philo *philo) { uint32_t timestamp; struct timeval t1; + static bool dead = false; static pthread_mutex_t print = {0}; gettimeofday(&t1, NULL); pthread_mutex_lock(&print); timestamp = get_time_in_ms(philo->t0, t1); - if (philo->state == EAT) + if (philo->state == EAT && dead == false) printf("%u %hu %s", timestamp, philo->id, EATING_MSG); - else if (philo->state == THINK) + else if (philo->state == THINK && dead == false) printf("%u %hu %s", timestamp, philo->id, THINK_MSG); - else if (philo->state == SLEEP) + else if (philo->state == SLEEP && dead == false) printf("%u %hu %s", timestamp, philo->id, SLEEP_MSG); - else if (philo->state == DEAD) + else if (philo->state == DEAD && dead == false) + { + dead = true; printf("%u %hu %s", timestamp, philo->id, DIED_MSG); - else if (philo->state == FORK_TAKEN) + } + else if (philo->state == FORK_TAKEN && dead == false) printf("%u %hu %s", timestamp, philo->id, FORK_MSG); pthread_mutex_unlock(&print); }