/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */
-/* Updated: 2024/03/19 11:31:32 by ljiriste ### ########.fr */
+/* Updated: 2024/03/19 11:55:01 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return ;
}
-int philo_eat(t_philo *philo)
+void philo_eat(t_philo *philo)
{
pthread_mutex_lock(philo->forks[0]);
thread_print(philo->settings, philo->id, "has taken a fork");
thread_print(philo->settings, philo->id, "is eating");
philo->last_eaten = cur_time_sus();
usleep(philo->settings->time_to_eat);
+ ++philo->times_eaten;
pthread_mutex_unlock(philo->forks[0]);
pthread_mutex_unlock(philo->forks[1]);
- return (0);
+ return ;
}
void *be_a_philosopher(void *inp)
while (!philo->settings->end)
{
philo_think(philo->settings, philo->id);
- if (philo_eat(philo) == 1)
- break ;
- ++philo->times_eaten;
- if (philo->times_eaten == philo->settings->min_eats_num)
- return (NULL);
+ philo_eat(philo);
philo_sleep(philo->settings, philo->id);
}
return (NULL);
suseconds_t oldest;
suseconds_t now;
size_t i;
+ int all_full;
while (1)
{
i = 0;
now = cur_time_sus();
+ all_full = set->min_eats_num > 0;
+ oldest = philos[0].last_eaten;
while (i < set->philo_count)
{
- oldest = philos[0].last_eaten;
if((now - philos[i].last_eaten) > set->time_to_die)
{
pthread_mutex_lock(&set->terminal);
}
if (oldest > philos[i].last_eaten)
oldest = philos[i].last_eaten;
+ all_full = (all_full && (philos[i].times_eaten >= set->min_eats_num));
++i;
}
+ if (all_full)
+ {
+ pthread_mutex_lock(&set->terminal);
+ printf("%li All philosophers are well fed\n", (cur_time_sus() - set->program_start) / 1000);
+ set->end = 1;
+ pthread_mutex_unlock(&set->terminal);
+ return ;
+ }
now = cur_time_sus();
//usleep(now - oldest);
}