Blame view

Threads/Exemples/Plusieur_args/main.c 920 Bytes
0ae69087   pfrison   Ajout des fichiers
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
  #include <stdio.h>
  #include <pthread.h>
  
  struct deux_int {
      int a;
      int b;
  };
  
  void *print_hello(void *arg){
      (void) arg; // ici on utilise pas l'argument
      printf("Hello\n");
      pthread_exit(NULL);
  }
  
  void *print_nombre(void *arg){
      struct deux_int ints = *(struct deux_int *) arg; // void* -> struct deux_int* -> struct deux_int
      printf("Les nombres sont :%d: et :%d:\n", ints.a, ints.b);
      pthread_exit(NULL);
  }
  
  int main(void){
      pthread_t tid1, tid2;
      // thread 1
      pthread_create(&tid1, NULL, print_hello, NULL);
      pthread_detach(tid1);
  
      //thread 2
      struct deux_int ints;
      ints.a = 15;
      ints.b = 7;
      pthread_create(&tid2, NULL, print_nombre, (void *) &ints); // struct deux_int -> struct deux_int* -> void*
      pthread_detach(tid2);
  
      printf("Je peux terminer le thread principal sans avoir à attendre les deux autre en mode détaché\n");
      pthread_exit(NULL);
  }