Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. // Fonctions correspondant au corps d’un thread (tache)
  7. void *etoile(void *inutilise);
  8. void *diese(void *inutilise);
  9. // Remarque : le prototype d’une tâche doit être : void *(*start_routine)(void *)
  10. int main(void)
  11. {
  12. pthread_t thrEtoile, thrDiese; // les ID des de 2 threads
  13.  
  14. setbuf(stdout, NULL); // pas de tampon sur stdout
  15. std ::cout << "Je vais creer et lancer 2 threads\n";
  16.  
  17. pthread_create(&thrEtoile, NULL, etoile, NULL);
  18. pthread_create(&thrDiese, NULL, diese, NULL);
  19.  
  20. //printf("J’attends la fin des 2 threads\n");
  21. pthread_join(thrEtoile, NULL);
  22. pthread_join(thrDiese, NULL);
  23. std ::cout << "Les deux thread se sont terminer.";
  24. std ::cout << "Fin du thread principal\n";
  25. pthread_exit(NULL);
  26. return EXIT_SUCCESS;
  27. }
  28.  
  29.  
  30. void *etoile(void *inutilise)
  31. {
  32. int i;
  33. char c1='*';
  34. for(i=1;i<=200;i++)
  35. {
  36. usleep(10);
  37. write(1, &c1, 1); // écrit un caractère sur stdout (descripteur 1)
  38. }
  39. return NULL;
  40. }
  41.  
  42.  
  43. void *diese(void *inutilise)
  44. {
  45. int i;
  46. char c1='#';
  47. for(i=1;i<=200;i++)
  48. {
  49. usleep(10);
  50. write(1, &c1, 1);
  51. }
  52. return NULL;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement