1
0

」 feat: seems to be working

This commit is contained in:
2024-08-12 20:52:02 +02:00
parent ec9ee12c29
commit 471ca75712
13 changed files with 219 additions and 206 deletions

View File

@ -6,32 +6,41 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 17:29:51 by adjoly #+# #+# */
/* Updated: 2024/08/08 18:05:07 by adjoly ### ########.fr */
/* Updated: 2024/08/12 20:50:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void take_fork(t_fork *fork, int id)
{
if (id % 2)
pthread_mutex_lock(&fork->left);
else if (!(id % 2))
pthread_mutex_lock(fork->right);
}
bool philo_eat(t_philo *philo)
{
philo->state = FORK_TAKEN;
take_fork(&philo->fork, philo->id);
if (get_death(false, true, philo))
if (get_death(philo, RETURN))
return (true);
log_philo(philo);
if (&(philo->fork.left) == philo->fork.right)
return (true);
return (print_death(philo));
take_fork(&philo->fork, philo->id + 1);
if (get_death(false, true, philo))
return (true);
// if (get_death(philo, RETURN))
// return (true);
log_philo(philo);
philo->state = EAT;
gettimeofday(&(philo->eat), NULL);
log_philo(philo);
gettimeofday(&philo->eat, NULL);
if (sleep_phil(philo) == true)
return (true);
pthread_mutex_unlock(&philo->fork.left);
pthread_mutex_unlock(philo->fork.right);
philo->meal_left--;
if (philo->data.no_meal == false && philo->meal_left > 0)
philo->meal_left--;
return (false);
}