From: Lukas Jiriste Date: Thu, 9 May 2024 09:19:16 +0000 (+0200) Subject: Add missing index increment, change cleanup order X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=a4c72f1bc98b22975c5eeefc9ebcd74658e0616d;p=42%2Fphilosophers.git Add missing index increment, change cleanup order The program only checked the forst philo for starvation. The change in the cleanup order is needed because without the change (forks) mutexes were deinitialized before joining the (philo) threads. This could lead to a philo waiting for a fork, which has just been cleared, leading to undefined behavior (endless wait). When the main thread joins all the threads it gets gets stuck in the one waitin for a fork. --- diff --git a/philo/main.c b/philo/main.c index 97ef11b..c19c92a 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 10:24:12 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 11:15:59 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,6 +67,7 @@ void watch_philosophers(t_diner *diner) print_timestamp(diner->philos + i, "died"); return ; } + ++i; } } } diff --git a/philo/mem_management.c b/philo/mem_management.c index aaa9464..0b245b4 100644 --- a/philo/mem_management.c +++ b/philo/mem_management.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 11:05:18 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 11:18:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,16 +54,16 @@ void cleanup(t_diner *diner, pthread_t *threads) destroy_mutex(&diner->setting.terminal_lock); destroy_mutex(&diner->setting.end_lock); i = 0; - if (diner->forks) - while (i < count) - destroy_mutex(diner->forks + (i++)); - i = 0; if (diner->philos) while (i < count) { pthread_join(threads[i], NULL); destroy_mutex(&diner->philos[i++].philo_lock); } + i = 0; + if (diner->forks) + while (i < count) + destroy_mutex(diner->forks + (i++)); free(diner->philos); free(diner->forks); free(threads);