48 lines
1.5 KiB
C
48 lines
1.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* threads.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
|
|
/* Updated: 2023/03/12 16:57:52 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "philo.h"
|
|
#include <pthread.h>
|
|
|
|
int manage_threads(t_config *config)
|
|
{
|
|
pthread_t *threads;
|
|
t_philo philo;
|
|
int i;
|
|
|
|
threads = malloc(config->nbr_philo * sizeof(pthread_t));
|
|
if (threads == NULL)
|
|
return (1);
|
|
i = -1;
|
|
philo.conf = *config;
|
|
while (++i < config->nbr_philo)
|
|
{
|
|
philo.id = i;
|
|
if (pthread_create(&threads[i], NULL, (void *)&philosopher, &philo) != 0)
|
|
{
|
|
write(2, "Error thread creation\n", 21);
|
|
return (1);
|
|
}
|
|
write(1, "Thread created\n", 15);
|
|
}
|
|
while (--i > -1)
|
|
{
|
|
if (pthread_join(threads[i], NULL) != 0)
|
|
{
|
|
write(2, "Error thread finish\n", 20);
|
|
return (1);
|
|
}
|
|
write(1, "Thread finish execution\n", 24);
|
|
}
|
|
return (0);
|
|
}
|