-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.c
100 lines (89 loc) · 2.44 KB
/
threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tkavisha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/05 12:49:38 by tkavisha #+# #+# */
/* Updated: 2023/09/05 18:06:31 by tkavisha ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int create_threads(t_data *data, t_philo *philo)
{
int i;
i = 0;
if (pthread_create(&data->threads_monitor_tid,
NULL, &threads_monitor, philo) != 0)
{
printf("Error : pthread_create");
clean_mutex(philo);
free_allocated_memory(data, philo);
return (1);
}
while (i < data->num_of_philo)
{
if (pthread_create(&philo[i].tid, NULL, &philo_routine, &philo[i]) != 0)
{
printf("Error : pthread_create");
create_threads_error(data, philo, i);
clean_mutex(philo);
free_allocated_memory(data, philo);
return (1);
}
i++;
}
return (0);
}
void create_threads_error(t_data *data, t_philo *philo, int until)
{
int i;
i = 0;
pthread_mutex_lock(&data->lock_dead);
*(data->dead) = 1;
pthread_mutex_unlock(&data->lock_dead);
while (i < until)
{
pthread_join(philo[i].tid, NULL);
i++;
}
}
void *threads_monitor(void *philos)
{
t_philo *philo;
philo = (t_philo *)philos;
while (1)
{
if (check_if_any_dead(philo) == 1 || check_if_philos_feed(philo) == 1)
break ;
}
return (NULL);
}
void join_threads(t_data *data, t_philo *philo)
{
int i;
if (pthread_join(data->threads_monitor_tid, NULL) != 0)
write(2, "Thread join error\n", 18);
i = 0;
while (i < data->num_of_philo)
{
if (pthread_join(philo[i].tid, NULL) != 0)
write(2, "Thread join error\n", 18);
i++;
}
}
void *philo_routine(void *void_philo)
{
t_philo *philo;
philo = (t_philo *)void_philo;
if (philo->num % 2 == 0)
my_usleep(1);
while (check_var_dead(philo) == 0)
{
philo_think(philo);
philo_eat(philo);
philo_sleep(philo);
}
return (NULL);
}