check died dans un autre thread et reorganisation fichiers avec main thread
This commit is contained in:
14
srcs/exec.c
14
srcs/exec.c
@@ -25,16 +25,18 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
|
||||
int eat(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
||||
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead))
|
||||
return (ret_err_unlock(philo, 1));
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
||||
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating") == 1)
|
||||
if (print_message(philo, B_YELLOW, "is eating", philo->global->dead))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
philo->eat++;
|
||||
if (philo->eat == philo->params->n_eat)
|
||||
philo->global->n_eat++;
|
||||
go_sleep(philo, philo->params->t_eat);
|
||||
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
@@ -49,15 +51,15 @@ void *philo_exec(void *arg)
|
||||
philo = (t_philo*)arg;
|
||||
init_time(philo);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
usleep(10);
|
||||
while (1)
|
||||
{
|
||||
if (eat(philo) != 0)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping") == 1)
|
||||
if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead))
|
||||
break ;
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
if (print_message(philo, B_GREEN, "is thinking") == 1)
|
||||
if (print_message(philo, B_GREEN, "is thinking", philo->global->dead))
|
||||
break ;
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
Reference in New Issue
Block a user