42_Philosopher/print.c
2023-05-17 13:25:34 +02:00

57 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:22:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include "./time.h"
#include "./utils/utils.h"
static void print(t_data *data, size_t id, char *str)
{
size_t time;
bool stop;
pthread_mutex_lock(&data->print_mutex);
pthread_mutex_lock(&data->stop_mutex);
stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex);
if (stop == false)
{
time = get_time();
printf("%zu %zu %s\n", time, id, str);
}
pthread_mutex_unlock(&data->print_mutex);
}
void print_take_a_fork(t_philo *philo)
{
print(philo->data, philo->id, "has taken a fork");
}
void print_eating(t_philo *philo)
{
print(philo->data, philo->id, "is eating");
}
void print_sleeping(t_philo *philo)
{
print(philo->data, philo->id, "is sleeping");
}
void print_thinking(t_philo *philo)
{
print(philo->data, philo->id, "is thinking");
}