41 lines
1.5 KiB
C
41 lines
1.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* threads.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/22 15:42:23 by lfirmin #+# #+# */
|
|
/* Updated: 2024/11/22 15:44:44 by lfirmin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include "../include/philo.h"
|
|
|
|
int create_threads(t_data *data)
|
|
{
|
|
int i;
|
|
pthread_t *threads;
|
|
|
|
i = 0;
|
|
threads = malloc(sizeof(pthread_t) * data->nb_philo);
|
|
if (!threads)
|
|
return (1);
|
|
while (i < data->nb_philo)
|
|
{
|
|
if (pthread_create(&data->philos[i].thread,
|
|
NULL, &philo_routine, &data->philos[i]))
|
|
{
|
|
pthread_mutex_lock(&data->dead_mutex);
|
|
data->dead_flag = 1;
|
|
pthread_mutex_unlock(&data->dead_mutex);
|
|
return (free(threads), 1);
|
|
}
|
|
threads[i] = data->philos[i].thread;
|
|
i++;
|
|
}
|
|
i = 0;
|
|
while (i < data->nb_philo)
|
|
pthread_join(threads[i++], NULL);
|
|
return (free(threads), 0);
|
|
}
|