42_Philosopher/philo/philo.c
Camille Chauvet aefb6ceec0 bozosujet
2023-05-17 13:28:42 +02:00

48 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:38:52 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:56:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "./print.h"
#include "philo.h"
#include "time.h"
#include "data.h"
void philo_destroyer(t_philo *philo)
{
pthread_mutex_destroy(&philo->nb_meal_mutex);
pthread_mutex_destroy(&philo->last_eat_mutex);
free(philo);
}
t_philo *philo_init(t_data *data)
{
t_philo *philo;
static size_t id = 0;
philo = malloc(sizeof(t_philo));
if (philo == NULL)
return (NULL);
philo->id = id++;
philo->data = data;
philo->nb_meal = 0;
philo->stop = 0;
philo->last_eat = get_time();
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
return (philo);
}