core: transition from bool[] to mutex[] for the fork tab

This commit is contained in:
Camille Chauvet 2023-04-25 14:32:42 +00:00
parent 4f138b78e3
commit 07d4cd05c1

19
data.c
View File

@ -6,13 +6,14 @@
#include "./data.h" #include "./data.h"
#include "philo.h" #include "philo.h"
#include "philos.h" #include "philos.h"
#include "struct.h"
#include <string.h> #include <string.h>
bool data_init(t_data *data) bool data_init(t_data *data)
{ {
size_t i;
data->stop = 0; 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) if (data->forks == NULL)
return (1); return (1);
memset(data->forks, 1, data->nb_philos); memset(data->forks, 1, data->nb_philos);
@ -29,7 +30,12 @@ bool data_init(t_data *data)
free(data->forks); free(data->forks);
return (1); 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->stop_mutex, NULL);
pthread_mutex_init(&data->print_mutex, NULL); pthread_mutex_init(&data->print_mutex, NULL);
return (0); return (0);
@ -52,7 +58,12 @@ void data_destroyer(t_data *data)
i++; i++;
usleep(1000); 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->stop_mutex);
pthread_mutex_destroy(&data->print_mutex); pthread_mutex_destroy(&data->print_mutex);
free(data->threads); free(data->threads);