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.
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
print_timestamp(diner->philos + i, "died");
return ;
}
+ ++i;
}
}
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
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);