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

52
srcs/main_thread.c Normal file
View File

@@ -0,0 +1,52 @@
#include "philo.h"
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->n_eat == philo->params->n_phi)
{
philo->global->dead = 1;
return (1);
}
gettimeofday(&stime, NULL);
time = diff_time(philo->t_last_meal, stime);
if (time >= philo->params->t_die)
{
philo->global->dead = 1;
print_message(philo, B_RED, "died", 0);
return (1);
}
return (0);
}
void launch_threads(t_philo *philo, pthread_t *id)
{
int i;
i = 0;
while (i < philo->params->n_phi)
{
pthread_create(&id[i], NULL, &philo_exec, philo);
philo = philo->next;
i++;
}
while (philo->global->dead == 0)
{
is_dead(philo);
philo = philo->next;
}
i = 0;
while (i < philo->params->n_phi)
{
pthread_create(&id[i], NULL, &philo_exec, philo);
philo = philo->next;
i++;
}
}