check died dans un autre thread et reorganisation fichiers avec main thread

This commit is contained in:
hugogogo
2022-01-26 10:52:06 +01:00
parent 03633fa5b2
commit 6561340fb9
8 changed files with 75 additions and 59 deletions

View File

@@ -21,51 +21,20 @@ void update_time(t_philo *philo)
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time old, struct timeval new)
{
return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000);
}
int is_dead(t_philo *philo, struct timeval stime)
{
int time;
if (philo->global->dead)
return (-1);
if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat)
{
philo->global->dead = 1;
return (philo->p_nbr);
}
time = diff_time(philo->t_last_meal, stime);
if (time >= philo->params->t_die)
{
philo->global->dead = 1;
return (philo->p_nbr);
}
return (0);
}
int print_message(t_philo *philo, char *clr, char *msg)
int print_message(t_philo *philo, char *clr, char *msg, int key)
{
long int time_stamp;
struct timeval stime;
char p_dead;
if (key)
return (1);
pthread_mutex_lock(&(philo->global->m_print));
gettimeofday(&stime, NULL);
p_dead = is_dead(philo, stime);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
if (p_dead != 0)
{
if (p_dead > 0)
ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
else
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
if (philo->global->dead)
return (1);
return (0);
}