-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_0_threads.c
84 lines (75 loc) · 2.18 KB
/
3_0_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 3_0_threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anhigo-s <anhigo-s@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/21 03:22:34 by anhigo-s #+# #+# */
/* Updated: 2022/03/30 13:56:48 by anhigo-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
static void create_mutex(t_philo *data);
static int create_threads(t_philo *data);
static void join_threads(t_philo *data);
void start_threads(t_philo *data)
{
data->list = start_list(data);
create_mutex(data);
if (!create_threads(data))
{
printf("philosophers: thread error\n");
}
join_threads(data);
return ;
}
static void create_mutex(t_philo *data)
{
t_thinker *temp;
int index;
temp = data->list;
index = data->list->list_size;
pthread_mutex_init(&data->printer_mutex, NULL);
while (index > 0)
{
pthread_mutex_init(&temp->eat_mutex, NULL);
pthread_mutex_init(&temp->fork, NULL);
index--;
temp = temp->next;
}
return ;
}
static int create_threads(t_philo *data)
{
t_thinker *temp;
int index;
temp = data->list;
index = data->list->list_size;
while (index > 0)
{
temp->last_meal = 0;
if (pthread_create(&(temp->thread), NULL, &main_routine, (void *)temp))
{
return (0);
}
index--;
temp = temp->prev;
}
pthread_create(&(data->death), NULL, &death_routine, (void *)data->list);
return (1);
}
static void join_threads(t_philo *data)
{
t_thinker *temp;
int index;
temp = data->list;
index = data->list->list_size;
pthread_join(temp->data->death, NULL);
while (index > 0)
{
pthread_join(temp->thread, NULL);
index--;
temp = temp->next;
}
}