aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src/routine.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-10 13:34:50 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-10 13:40:02 +0100
commitd552a2af95a3e03f937a94945369536d1eaae74a (patch)
treedcbc6bb1af33219730086cfb5ca1ce0b83a17187 /philo_one/src/routine.c
parent5b03a0e341881ace66dc5cec5dcfba82fce8221d (diff)
downloadphilosophers-d552a2af95a3e03f937a94945369536d1eaae74a.tar.gz
philosophers-d552a2af95a3e03f937a94945369536d1eaae74a.tar.bz2
philosophers-d552a2af95a3e03f937a94945369536d1eaae74a.zip
Updated philo_two with better setup, Norming philo_two
Diffstat (limited to 'philo_one/src/routine.c')
-rw-r--r--philo_one/src/routine.c35
1 files changed, 18 insertions, 17 deletions
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);
}