aboutsummaryrefslogtreecommitdiff
path: root/philo_one/src/routine.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-10 10:54:04 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-10 10:54:04 +0100
commiteb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb (patch)
treeefff80bbbc2157979eaae006f6835d020062dba9 /philo_one/src/routine.c
parent802ea8347d1ceb7a02cf279359b3b101106fab94 (diff)
downloadphilosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.tar.gz
philosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.tar.bz2
philosophers-eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb.zip
Fixing bad performance at school by adding more delay to the death checking loop, Added mutex/semaphore to protect against eating and dying at the same time
Diffstat (limited to 'philo_one/src/routine.c')
-rw-r--r--philo_one/src/routine.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/philo_one/src/routine.c b/philo_one/src/routine.c
index 340f3b7..01c74d5 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/09 14:32:33 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:54:30 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -67,9 +67,14 @@ void *routine_philo(t_philo *arg)
void *routine_death(t_philo *arg)
{
- while (!philo_finished(arg->conf) &&
- h_time_now() - arg->time_last_eat < arg->conf->timeout_death)
- usleep(1000);
+ 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);
}