From 907debbb7d1e7ccc4914805cfe4acbed92b82bcc Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 4 Jan 2021 12:20:25 +0100 Subject: Added buffered output, Added waiting for all to be started --- philo_one/src/event.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'philo_one/src/event.c') diff --git a/philo_one/src/event.c b/philo_one/src/event.c index 8ceab65..2e24772 100644 --- a/philo_one/src/event.c +++ b/philo_one/src/event.c @@ -6,21 +6,25 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2021/01/03 16:48:00 by cacharle ### ########.fr */ +/* Updated: 2021/01/04 12:08:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void event_take_fork(t_philo *arg, pthread_mutex_t *fork) +void event_take_fork(t_philo *arg) { if (philo_finished(arg->conf)) return ; - pthread_mutex_lock(fork); + pthread_mutex_lock(arg->fork_left); + pthread_mutex_lock(arg->fork_right); pthread_mutex_lock(&arg->conf->mutex_stdout); if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); + if (philo_finished(arg->conf)) + return ; + philo_put(arg->id, EVENT_FORK, arg->conf->initial_time); pthread_mutex_unlock(&arg->conf->mutex_stdout); } @@ -32,6 +36,8 @@ void event_eat(t_philo *arg) if (philo_finished(arg->conf)) return ; philo_put(arg->id, EVENT_EAT, arg->conf->initial_time); + if (arg->conf->philo_num < 30) + philo_put_flush(); pthread_mutex_unlock(&arg->conf->mutex_stdout); h_sleep(arg->conf->timeout_eat); } @@ -69,9 +75,6 @@ void event_die(t_philo *arg) if (philo_finished(arg->conf)) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); - if (philo_finished(arg->conf)) - return ; - arg->conf->all_alive = false; philo_put(arg->id, EVENT_DIE, arg->conf->initial_time); - pthread_mutex_unlock(&arg->conf->mutex_stdout); + arg->conf->all_alive = false; } -- cgit