fix print mutex is now destroyed at the end

This commit is contained in:
Camille Chauvet 2023-04-20 12:40:30 +00:00
parent 32d881a7d2
commit 26e9f21238
3 changed files with 5 additions and 3 deletions

2
data.c
View File

@ -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);

View File

@ -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)

View File

@ -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