42_Philosopher/philo/philo.c

48 lines
1.6 KiB
C
Raw Normal View History

2023-04-27 08:05:14 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
2023-04-13 09:00:39 -04:00
#include <pthread.h>
#include <stdbool.h>
2023-04-19 07:53:50 -04:00
#include <stddef.h>
#include <stdio.h>
2023-04-13 09:00:39 -04:00
#include <unistd.h>
#include "./print.h"
2023-04-25 10:33:35 -04:00
#include "philo.h"
#include "time.h"
#include "data.h"
2023-04-13 09:00:39 -04:00
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;
2023-04-20 12:27:12 -04:00
philo->stop = 0;
2023-04-19 07:53:50 -04:00
philo->last_eat = get_time();
2023-04-13 09:00:39 -04:00
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
2023-04-19 07:53:50 -04:00
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
2023-04-13 09:00:39 -04:00
return (philo);
}