aboutsummaryrefslogtreecommitdiff
path: root/philo_one
diff options
context:
space:
mode:
Diffstat (limited to 'philo_one')
-rw-r--r--philo_one/src/event.c5
-rw-r--r--philo_one/src/main.c2
-rw-r--r--philo_one/src/philo.c3
-rw-r--r--philo_one/src/philo_one.h3
-rw-r--r--philo_one/src/routine.c13
5 files changed, 17 insertions, 9 deletions
diff --git a/philo_one/src/event.c b/philo_one/src/event.c
index 92ca399..88b9ff1 100644
--- a/philo_one/src/event.c
+++ b/philo_one/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */
-/* Updated: 2021/01/09 13:12:20 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:55:58 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,14 +28,15 @@ void event_eat(t_philo *arg)
{
if (philo_finished(arg->conf))
return ;
+ pthread_mutex_lock(&arg->mutex_eat);
pthread_mutex_lock(&arg->conf->mutex_stdout);
if (philo_finished(arg->conf))
return ;
philo_put(arg->id, EVENT_EAT, arg->conf->initial_time);
pthread_mutex_unlock(&arg->conf->mutex_stdout);
arg->time_last_eat = h_time_now();
+ pthread_mutex_unlock(&arg->mutex_eat);
h_sleep(arg->conf->timeout_eat);
- arg->time_last_eat = h_time_now();
}
void event_think(t_philo *arg)
diff --git a/philo_one/src/main.c b/philo_one/src/main.c
index d02fb7a..3dacf8f 100644
--- a/philo_one/src/main.c
+++ b/philo_one/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */
-/* Updated: 2021/01/09 14:26:48 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:33:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/philo_one/src/philo.c b/philo_one/src/philo.c
index 911f6b9..8a41fe7 100644
--- a/philo_one/src/philo.c
+++ b/philo_one/src/philo.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */
-/* Updated: 2021/01/09 14:39:03 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:49:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,6 +30,7 @@ t_philo *philos_new(t_philo_conf *conf, pthread_mutex_t *forks)
philos[i].fork_right = forks + (i + 1) % conf->philo_num;
pthread_mutex_init(&philos[i].mutex_start, NULL);
pthread_mutex_lock(&philos[i].mutex_start);
+ pthread_mutex_init(&philos[i].mutex_eat, NULL);
}
return (philos);
}
diff --git a/philo_one/src/philo_one.h b/philo_one/src/philo_one.h
index 910dfe8..aeabc4c 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/09 14:26:58 by charles ### ########.fr */
+/* Updated: 2021/01/10 09:50:02 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,6 +52,7 @@ typedef struct s_philo
pthread_mutex_t *fork_right;
pthread_mutex_t *mutex_stdout;
pthread_mutex_t mutex_start;
+ pthread_mutex_t mutex_eat;
} t_philo;
/*
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);
}