1
0

🔨」 fix: print after death

This commit is contained in:
2024-08-13 12:25:24 +02:00
parent 4b0799c8e5
commit ecc704da7f

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:12:20 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; uint32_t timestamp;
struct timeval t1; struct timeval t1;
static bool dead = false;
static pthread_mutex_t print = {0}; static pthread_mutex_t print = {0};
gettimeofday(&t1, NULL); gettimeofday(&t1, NULL);
pthread_mutex_lock(&print); pthread_mutex_lock(&print);
timestamp = get_time_in_ms(philo->t0, t1); 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); 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); 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); 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); 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); printf("%u %hu %s", timestamp, philo->id, FORK_MSG);
pthread_mutex_unlock(&print); pthread_mutex_unlock(&print);
} }