/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* philo_threads.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ /* Updated: 2022/05/16 17:51:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "philosophers.h" void *philos_life(void *r) { t_room *room; t_param *param; t_philo *philo; int id; int id_p; room = (t_room *)r; param = room->param; pthread_mutex_lock(room->safe); id = room->i; philo = room->philos[id]; philo->eat_time = gettime(); id_p = (id + 1) % param->philo_amount; while (room->running) { pthread_mutex_unlock(room->safe); pthread_mutex_lock(philo->fork); safe_print("%ld %d has taken a fork\n", room, id); pthread_mutex_lock(room->philos[id_p]->fork); safe_print("%ld %d has taken a fork\n", room, id); safe_print("%ld %d is eating\n", room, id); if (!safe_wait(room, gettime(), param->eat_duration * 1000)) return (NULL); pthread_mutex_unlock(philo->fork); pthread_mutex_unlock(room->philos[id_p]->fork); pthread_mutex_lock(philo->safe); philo->eat_time = gettime(); philo->eat_amount++; pthread_mutex_unlock(philo->safe); safe_print("%ld %d is sleeping\n", room, id); if (!safe_wait(room, gettime(), param->sleep_duration * 1000)) return (NULL); safe_print("%ld %d is thinking\n", room, id); pthread_mutex_lock(room->safe); } pthread_mutex_unlock(room->safe); return (NULL); }