Adding something working

This commit is contained in:
EniumRaphael 2024-07-11 18:11:14 +02:00
parent af63bafed0
commit 5215c0e3f4
4 changed files with 50 additions and 31 deletions

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* */
/* ::: :::::::: */
/* routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
/* Updated: 2024/07/04 13:29:39 by rparodi ### ########.fr */
/* Updated: 2024/07/11 18:10:49 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,10 +32,10 @@ t_bool check_dead(t_philo *philo)
timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat;
if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death)
{
pthread_mutex_unlock(philo[i].dead_lock);
pthread_mutex_lock(philo[i].dead_lock);
ft_logs("died\n", &philo[i]);
philo[i].dead_check = true;
pthread_mutex_lock(philo[i].dead_lock);
pthread_mutex_unlock(philo[i].dead_lock);
return (true);
}
i++;
@ -60,37 +60,27 @@ void counting_launch(t_philo *philo, t_u8 *counter)
t_bool check_eat(t_philo *philo)
{
t_usize i;
t_u8 check;
i = 0;
check = 0;
if (philo[0].nb_eat == -1)
return (false);
counting_launch(philo, &check);
if (check == philo[0].nb_philo)
{
pthread_mutex_lock(philo[0].dead_lock);
while (i < philo[0].nb_philo)
{
pthread_mutex_unlock(philo[i].dead_lock);
philo[i].dead_check = true;
pthread_mutex_lock(philo[i].dead_lock);
i++;
}
pthread_mutex_unlock(philo[0].dead_lock);
return (true);
}
return (false);
}
void *ft_watch_dogs(void *ptr)
{
t_philo *philo;
t_usize i;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
i = philo[0].nb_philo;
pthread_mutex_lock(philo->print_lock);
printf("\n\nEntre dans ft_watch_dogs\n\n");
pthread_mutex_unlock(philo->print_lock);
while (true)
{
if (check_eat(philo) == true)
@ -98,5 +88,14 @@ void *ft_watch_dogs(void *ptr)
if (check_dead(philo) == true)
break ;
}
pthread_mutex_lock(philo->print_lock);
printf("\n\nSortie de ft_watch_dogs\n\n");
pthread_mutex_unlock(philo->print_lock);
while (i-- != 0)
{
pthread_mutex_lock(philo[i].dead_lock);
philo[i].dead_check = true;
pthread_mutex_unlock(philo[i].dead_lock);
}
return (philo);
}