50 lines
1005 B
C
50 lines
1005 B
C
#include "philo.h"
|
|
#include <bits/pthreadtypes.h>
|
|
#include <pthread.h>
|
|
#include <stddef.h>
|
|
#include "./time.h"
|
|
#include "struct.h"
|
|
|
|
static void print(t_data *data, size_t id, char *str)
|
|
{
|
|
static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
size_t time;
|
|
|
|
pthread_mutex_lock(&data->stop_mutex);
|
|
if (data->stop)
|
|
{
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
|
return ;
|
|
}
|
|
pthread_mutex_unlock(&data->stop_mutex);
|
|
time = get_time();
|
|
pthread_mutex_lock(&print_mutex);
|
|
printf("%07zu %3zu %s\n", time, id + 1, str);
|
|
pthread_mutex_unlock(&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");
|
|
}
|
|
|
|
void print_died(t_philo *philo)
|
|
{
|
|
print(philo->data, philo->id, "died");
|
|
}
|