Staticify some more.
[TD_SE.git] / threads / exemple1.c
1 //exemple1.c
2
3 #define _REENTRANT
4 #include <pthread.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 static void afficher(int n, char lettre)
10 {
11 int j;
12 for (j = 1; j < n; j++) {
13 printf("%c", lettre);
14 fflush(stdout);
15 }
16 }
17
18 static void *threadA(void *inutilise)
19 {
20 afficher(100, 'A');
21 printf("\n Fin du thread A\n");
22 fflush(stdout);
23 pthread_exit(NULL);
24 }
25
26 static void *threadC(void *inutilise)
27 {
28 afficher(150, 'C');
29 printf("\n Fin du thread C\n");
30 fflush(stdout);
31 pthread_exit(NULL);
32 }
33
34 static void *threadB(void *inutilise)
35 {
36 pthread_t thC;
37 pthread_create(&thC, NULL, threadC, NULL);
38 afficher(100, 'B');
39 printf("\n Le thread B attend la fin du thread C\n");
40 pthread_join(thC, NULL);
41 printf("\n Fin du thread B\n");
42 fflush(stdout);
43 pthread_exit(NULL);
44 }
45
46 int main()
47 {
48 pthread_t thA, thB;
49 printf("Création du Thread A\n");
50 pthread_create(&thA, NULL, threadA, NULL);
51 pthread_create(&thB, NULL, threadB, NULL);
52 sleep(1); // simule un traitement
53 printf("Le processus principal attend que les autres se terminent\n");
54 // attendre la fin des threads
55 pthread_join(thA, NULL);
56 pthread_join(thB, NULL);
57 exit(0);
58 }