diff --git a/data.c b/data.c index 823346b..a19b58a 100644 --- a/data.c +++ b/data.c @@ -30,6 +30,7 @@ bool data_init(t_data *data) } pthread_mutex_init(&data->forks_mutex, NULL); pthread_mutex_init(&data->stop_mutex, NULL); + pthread_mutex_init(&data->print_mutex, NULL); data->stop = 0; return (0); } @@ -53,6 +54,7 @@ void data_destroyer(t_data *data) } pthread_mutex_destroy(&data->forks_mutex); pthread_mutex_destroy(&data->stop_mutex); + pthread_mutex_destroy(&data->print_mutex); free(data->threads); free(data->forks); philos_destroyer(data); diff --git a/print.c b/print.c index a408521..f93f491 100644 --- a/print.c +++ b/print.c @@ -7,7 +7,6 @@ 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); @@ -18,9 +17,9 @@ static void print(t_data *data, size_t id, char *str) } pthread_mutex_unlock(&data->stop_mutex); time = get_time(); - pthread_mutex_lock(&print_mutex); + pthread_mutex_lock(&data->print_mutex); printf("%07zu %03zu %s\n", time, id + 1, str); - pthread_mutex_unlock(&print_mutex); + pthread_mutex_unlock(&data->print_mutex); } void print_take_a_fork(t_philo *philo) diff --git a/struct.h b/struct.h index c7bd438..f586be4 100644 --- a/struct.h +++ b/struct.h @@ -17,6 +17,7 @@ typedef struct s_data bool *forks; pthread_mutex_t stop_mutex; bool stop; + pthread_mutex_t print_mutex; } t_data; typedef struct s_philo