From 07d4cd05c1c947a9b66372ced49ac99b8cfa56c4 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 25 Apr 2023 14:32:42 +0000 Subject: [PATCH] core: transition from bool[] to mutex[] for the fork tab --- data.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/data.c b/data.c index 8769c3a..6b1de35 100644 --- a/data.c +++ b/data.c @@ -6,13 +6,14 @@ #include "./data.h" #include "philo.h" #include "philos.h" -#include "struct.h" #include bool data_init(t_data *data) { + size_t i; + data->stop = 0; - data->forks = malloc(sizeof(bool) * data->nb_philos); + data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos); if (data->forks == NULL) return (1); memset(data->forks, 1, data->nb_philos); @@ -29,7 +30,12 @@ bool data_init(t_data *data) free(data->forks); return (1); } - pthread_mutex_init(&data->forks_mutex, NULL); + i = 0; + while (i < data->nb_philos) + { + pthread_mutex_init(&data->forks[i], NULL); + i++; + } pthread_mutex_init(&data->stop_mutex, NULL); pthread_mutex_init(&data->print_mutex, NULL); return (0); @@ -52,7 +58,12 @@ void data_destroyer(t_data *data) i++; usleep(1000); } - pthread_mutex_destroy(&data->forks_mutex); + i = 0; + while (i < data->nb_philos) + { + pthread_mutex_destroy(&data->forks[i]); + i++; + } pthread_mutex_destroy(&data->stop_mutex); pthread_mutex_destroy(&data->print_mutex); free(data->threads);