change name mtx_msg for m_print, malloc mutex instead of equal NULL, malloc id with pthread_t instead of int, and change name some functions

This commit is contained in:
hugogogo
2022-01-19 17:47:47 +01:00
parent f8f509dd59
commit ac9d619731
5 changed files with 13 additions and 15 deletions

View File

@@ -2,7 +2,7 @@ NAME = philo
CC = clang
CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3
VPATH = $(DIR_SRCS)
DIR_SRCS = srcs

View File

@@ -17,7 +17,7 @@ typedef struct s_philo
t_params *params;
int philo_nbr;
int fork;
t_mtx *msg_mtx;
t_mtx *m_print;
struct s_philo *prev;
struct s_philo *next;
} t_philo;

View File

@@ -24,11 +24,9 @@ void *philo_exec(void *arg)
philo = (t_philo*)arg;
nbr = philo->philo_nbr;
pthread_mutex_lock(philo->msg_mtx);
// pthread_mutex_lock(&mutex);
ft_printf("%i is thinking\n", philo->philo_nbr);
pthread_mutex_unlock(philo->msg_mtx);
// pthread_mutex_unlock(&mutex);
pthread_mutex_lock(philo->m_print);
ft_printf("%i is thinking\n", philo->philo_nbr);
pthread_mutex_unlock(philo->m_print);
// eat
// "has taken a fork"

View File

@@ -1,6 +1,6 @@
#include "philo.h"
t_philo *create_each_philo(t_philo *philo, t_params *params, t_mtx *mtx, int i)
t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i)
{
t_philo *new;
@@ -10,7 +10,7 @@ t_philo *create_each_philo(t_philo *philo, t_params *params, t_mtx *mtx, int i)
new->params = params;
new->philo_nbr = i + 1;
new->fork = 1;
new->msg_mtx = mtx;
new->m_print = m_print;
if (philo)
philo->next = new;
new->prev = philo;
@@ -18,7 +18,7 @@ t_philo *create_each_philo(t_philo *philo, t_params *params, t_mtx *mtx, int i)
}
// looping chained list
t_philo *init_chain_philo(t_params *params, t_mtx *mutex)
t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
{
t_philo *end;
t_philo *philo;
@@ -28,7 +28,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *mutex)
philo = NULL;
while (i < params->n_phi)
{
philo = create_each_philo(philo, params, mutex, i);
philo = lst_add_philo(philo, params, m_print, i);
i++;
}
end = philo;
@@ -67,10 +67,10 @@ t_philo *init(int ac, char **av, pthread_t **id)
params = init_params(ac, av);
if (params == NULL)
return (NULL);
*id = malloc(sizeof(int) * params->n_phi);
*id = malloc(sizeof(pthread_t) * params->n_phi);
if (*id == NULL)
return (NULL);
m_print = NULL;
m_print = malloc(sizeof(t_mtx));
if (pthread_mutex_init(m_print, NULL) != 0)
return (NULL);
philo = init_chain_philo(params, m_print);

View File

@@ -1,6 +1,6 @@
#include "philo.h"
void create_threads(t_philo *philo, pthread_t *id, int n)
void launch_threads(t_philo *philo, pthread_t *id, int n)
{
int i;
@@ -24,7 +24,7 @@ int main(int ac, char **av)
if (philo == NULL)
return (0);
n = philo->params->n_phi;
create_threads(philo, id, n);
launch_threads(philo, id, n);
i = 0;
while (i < n)
{