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:
2
Makefile
2
Makefile
@@ -2,7 +2,7 @@ NAME = philo
|
|||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|
||||||
CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3
|
||||||
|
|
||||||
VPATH = $(DIR_SRCS)
|
VPATH = $(DIR_SRCS)
|
||||||
DIR_SRCS = srcs
|
DIR_SRCS = srcs
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ typedef struct s_philo
|
|||||||
t_params *params;
|
t_params *params;
|
||||||
int philo_nbr;
|
int philo_nbr;
|
||||||
int fork;
|
int fork;
|
||||||
t_mtx *msg_mtx;
|
t_mtx *m_print;
|
||||||
struct s_philo *prev;
|
struct s_philo *prev;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|||||||
@@ -24,11 +24,9 @@ void *philo_exec(void *arg)
|
|||||||
philo = (t_philo*)arg;
|
philo = (t_philo*)arg;
|
||||||
nbr = philo->philo_nbr;
|
nbr = philo->philo_nbr;
|
||||||
|
|
||||||
pthread_mutex_lock(philo->msg_mtx);
|
pthread_mutex_lock(philo->m_print);
|
||||||
// pthread_mutex_lock(&mutex);
|
ft_printf("%i is thinking\n", philo->philo_nbr);
|
||||||
ft_printf("%i is thinking\n", philo->philo_nbr);
|
pthread_mutex_unlock(philo->m_print);
|
||||||
pthread_mutex_unlock(philo->msg_mtx);
|
|
||||||
// pthread_mutex_unlock(&mutex);
|
|
||||||
|
|
||||||
// eat
|
// eat
|
||||||
// "has taken a fork"
|
// "has taken a fork"
|
||||||
|
|||||||
12
srcs/init.c
12
srcs/init.c
@@ -1,6 +1,6 @@
|
|||||||
#include "philo.h"
|
#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;
|
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->params = params;
|
||||||
new->philo_nbr = i + 1;
|
new->philo_nbr = i + 1;
|
||||||
new->fork = 1;
|
new->fork = 1;
|
||||||
new->msg_mtx = mtx;
|
new->m_print = m_print;
|
||||||
if (philo)
|
if (philo)
|
||||||
philo->next = new;
|
philo->next = new;
|
||||||
new->prev = philo;
|
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
|
// 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 *end;
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
@@ -28,7 +28,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *mutex)
|
|||||||
philo = NULL;
|
philo = NULL;
|
||||||
while (i < params->n_phi)
|
while (i < params->n_phi)
|
||||||
{
|
{
|
||||||
philo = create_each_philo(philo, params, mutex, i);
|
philo = lst_add_philo(philo, params, m_print, i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
end = philo;
|
end = philo;
|
||||||
@@ -67,10 +67,10 @@ t_philo *init(int ac, char **av, pthread_t **id)
|
|||||||
params = init_params(ac, av);
|
params = init_params(ac, av);
|
||||||
if (params == NULL)
|
if (params == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
*id = malloc(sizeof(int) * params->n_phi);
|
*id = malloc(sizeof(pthread_t) * params->n_phi);
|
||||||
if (*id == NULL)
|
if (*id == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
m_print = NULL;
|
m_print = malloc(sizeof(t_mtx));
|
||||||
if (pthread_mutex_init(m_print, NULL) != 0)
|
if (pthread_mutex_init(m_print, NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
philo = init_chain_philo(params, m_print);
|
philo = init_chain_philo(params, m_print);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "philo.h"
|
#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;
|
int i;
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ int main(int ac, char **av)
|
|||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
n = philo->params->n_phi;
|
n = philo->params->n_phi;
|
||||||
create_threads(philo, id, n);
|
launch_threads(philo, id, n);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < n)
|
while (i < n)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user