From d552a2af95a3e03f937a94945369536d1eaae74a Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 10 Jan 2021 13:34:50 +0100 Subject: Updated philo_two with better setup, Norming philo_two --- philo_one/src/routine.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'philo_one/src/routine.c') diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c index 4e1f39f..4ba4844 100644 --- a/philo_one/src/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2021/01/10 11:52:44 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 12:05:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,20 @@ static void st_take_both_forks(t_philo *arg) } } +static void *st_routine_death(t_philo *arg) +{ + while (!philo_finished(arg->conf)) + { + pthread_mutex_lock(&arg->mutex_eat); + if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) + break ; + pthread_mutex_unlock(&arg->mutex_eat); + usleep(2000); + } + event_die(arg); + return (NULL); +} + void *routine_philo(t_philo *arg) { pthread_t thread_death; @@ -54,8 +68,10 @@ void *routine_philo(t_philo *arg) if (philo_finished(arg->conf)) return (NULL); arg->time_last_eat = h_time_now(); - if (pthread_create(&thread_death, NULL, (t_routine)routine_death, arg) != 0) + if (pthread_create(&thread_death, NULL, + (t_routine)st_routine_death, arg) != 0) return (NULL); + pthread_detach(thread_death); arg->time_last_eat = h_time_now(); while (!philo_finished(arg->conf)) { @@ -66,20 +82,5 @@ void *routine_philo(t_philo *arg) event_sleep(arg, arg->fork_right, arg->fork_left); event_think(arg); } - pthread_join(thread_death, NULL); - return (NULL); -} - -void *routine_death(t_philo *arg) -{ - while (!philo_finished(arg->conf)) - { - pthread_mutex_lock(&arg->mutex_eat); - if (h_time_now() - arg->time_last_eat > arg->conf->timeout_death) - break ; - pthread_mutex_unlock(&arg->mutex_eat); - usleep(2000); - } - event_die(arg); return (NULL); } -- cgit