reorganisation structs et timestamp global

This commit is contained in:
Hugo LAMY
2022-01-22 12:41:30 +01:00
parent 6a4aad9503
commit 3ba76ccc4e
5 changed files with 34 additions and 28 deletions

View File

@@ -7,21 +7,24 @@ void go_sleep(t_philo *philo, int time)
usleep(time * 1000);
}
int ret_unlock(t_mtx mutex, int ret)
int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret)
{
pthread_mutex_unlock(&mutex);
if (mutex_1)
pthread_mutex_unlock(mutex_1);
if (mutex_2)
pthread_mutex_unlock(mutex_2);
return (ret);
}
int take_forks(t_philo *philo)
{
pthread_mutex_lock(&(philo->m_fork));
if (philo->death)
return (ret_unlock(philo->m_fork, 1));
if (philo->params->dead != 0)
return (ret_unlock(&(philo->m_fork), NULL, 1));
print_message(philo, "has taken a fork");
pthread_mutex_lock(&(philo->next->m_fork));
if (philo->death)
return (ret_unlock(philo->next->m_fork, 1));
if (philo->params->dead != 0)
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
print_message(philo, "has taken a fork");
print_message(philo, "is eating");