-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexemple_create.c
50 lines (41 loc) · 997 Bytes
/
exemple_create.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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NB_THREADS 2
void * fn_thread(void * numero);
static int compteur = 0;
int
main(void) {
pthread_t thread [NB_THREADS];
int i;
int ret;
for (i = 0; i < NB_THREADS; i++)
if ((ret = pthread_create(& thread [i],
NULL,
fn_thread,
(void *) (intptr_t) i)) != 0) {
fprintf(stderr, "%s", strerror(ret));
exit(1);
}
while (1) {
fprintf(stdout, "main : compteur = %d\n", compteur);
sleep(1);
}
for (i = 0; i < NB_THREADS; i++)
pthread_join(thread [i], NULL);
return (0);
}
void *
fn_thread(void * num) {
int numero = (int) (intptr_t) num;
while (1) {
sleep((numero+1));
compteur++;
fprintf(stdout, "Thread %d : compteur = %d\n",
numero, compteur);
}
pthread_exit(NULL);
}