-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphilosophers.c
More file actions
50 lines (43 loc) · 1.52 KB
/
philosophers.c
File metadata and controls
50 lines (43 loc) · 1.52 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philosophers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mruggier <mruggier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 15:21:56 by mruggier #+# #+# */
/* Updated: 2024/01/17 18:17:40 by mruggier ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
void *do_philosophy(void *arg)
{
t_lock *lock = (t_lock *)arg;
pthread_mutex_lock(&lock->lock);
lock->ledger += 1;
pthread_mutex_unlock(&lock->lock);
return (NULL);
}
int main(int argc, char **argv)
{
t_lock lock;
pthread_t thread[100]; // array of threads forks
lock.ledger = 0;
int i;
pthread_mutex_init(&lock.lock, NULL);
i = 0;
while (i < 100)
{
pthread_create(thread + i, NULL, do_philosophy, (void *)&lock);
i++;
}
i = 0;
while (i < 100)
{
pthread_join(thread[i], NULL); // wait for thread to finish
i++;
}
printf ("ledger: %d\n", lock.ledger);
pthread_mutex_destroy(&lock.lock);
return (0);
}