diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-10 13:34:50 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-10 13:40:02 +0100 |
| commit | d552a2af95a3e03f937a94945369536d1eaae74a (patch) | |
| tree | dcbc6bb1af33219730086cfb5ca1ce0b83a17187 /philo_one | |
| parent | 5b03a0e341881ace66dc5cec5dcfba82fce8221d (diff) | |
| download | philosophers-d552a2af95a3e03f937a94945369536d1eaae74a.tar.gz philosophers-d552a2af95a3e03f937a94945369536d1eaae74a.tar.bz2 philosophers-d552a2af95a3e03f937a94945369536d1eaae74a.zip | |
Updated philo_two with better setup, Norming philo_two
Diffstat (limited to 'philo_one')
| -rw-r--r-- | philo_one/src/philo_one.h | 3 | ||||
| -rw-r--r-- | philo_one/src/routine.c | 35 |
2 files changed, 19 insertions, 19 deletions
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h index 2af457e..a327d2e 100644 --- a/philo_one/src/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2021/01/10 11:42:48 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 12:00:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,6 @@ void philos_destroy(t_philo *philos, long int num); bool philo_finished(t_philo_conf *conf); void *routine_philo(t_philo *arg); -void *routine_death(t_philo *arg); /* ** io.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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } |
