Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h> // printf etc.
  3. #include <stdlib.h>
  4.  
  5. ///////////////////////////////////////////////////////////////////////////////
  6. // Objectif du TP - découvrir les bases de la programmation concurrente et
  7. // et parallelles
  8. //
  9. // On va réaliser un compteur partagé
  10. //
  11. // Bonne référence sur le sujet:
  12. // https://randu.org/tutorials/threads/
  13. //
  14. // Exercices:
  15. // - Corriger les erreurs de compilation (pourquoi? Pour vous faire lire le fichier)
  16. // - Lancer plusieurs fois ce code observer les différences entre 8 lancements
  17. // - Commenter les fonctions dans le style des docstrings (resemble bcp au jsdoc).
  18. // -
  19. ///////////////////////////////////////////////////////////////////////////////
  20.  
  21. // Shared_counter functions and type definitions.
  22. typedef struct _shared_counter_t {
  23. int counter;
  24. } shared_counter_t;
  25.  
  26. shared_counter_t * shared_counter_new(void);
  27. void shared_counter_drop(shared_counter_t * sc);
  28. void shared_counter_increment(shared_counter_t * shared_counter);
  29. int shared_counter_value(const shared_counter_t * shared_counter);
  30.  
  31. typedef struct _thread_data_t {
  32. int thread_id;
  33. shared_counter_t * shared_counter;
  34. } thread_data_t;
  35.  
  36. /**
  37. * Create and allocate in heap a counter designed to be shared across threads.
  38. * @return pointer to heap allocated shared_counter_t.
  39. */
  40. shared_counter_t * shared_counter_new(void) {
  41. shared_counter_t * sc = malloc(sizeof(shared_counter_t));
  42. sc->counter = 0;
  43. return sc;
  44. }
  45.  
  46. /**
  47. * Handle destruction of a heap allocated shared counter.
  48. * ⚠ Beaware that threads might have an acces to this counter
  49. * after deletion!
  50. * @param sc Shared counter to delete.
  51. */
  52. void shared_counter_drop(shared_counter_t * sc) {
  53. // TODO: Might need some change to work really in the way we attend ;)
  54. free(sc);
  55. }
  56.  
  57. void shared_counter_increment(shared_counter_t * shared_counter) {
  58. // TODO: Might need something like a ???.
  59. shared_counter->counter += 1;
  60. }
  61.  
  62. int shared_counter_value(const shared_counter_t * shared_counter) {
  63. // TODO: Read need something or not?
  64. return shared_counter->counter;
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////
  67.  
  68. // Main program.
  69.  
  70. #define MAX_THREADS 3
  71. void * run(void * args) {
  72. thread_data_t * data = (thread_data_t *) args;
  73. printf("Bonjour depuis le thread: %i.\n", data->thread_id);
  74. // Let's work thread! Increment counter 10000 times.
  75. for (int i = 0; i < 10000; i++) {
  76. shared_counter_increment(data->shared_counter);
  77. }
  78. // End our thread it worked well, can gain some rest.
  79. pthread_exit(NULL);
  80. }
  81.  
  82. int main(void) {
  83. printf("The main thread is started\n");
  84.  
  85. // On crée un tableau pour les structures pthreads.
  86. pthread_t threads[MAX_THREADS];
  87. // on crée un tableau pour les data passées au thread.
  88. thread_data_t thr_data[MAX_THREADS];
  89.  
  90. // On crée une ressource partagée ici un compteur partagé
  91. // Et on va découvrir les problèmes de partager sans politique de partage ;)
  92. shared_counter_t * shared_counter = shared_counter_new();
  93.  
  94. // On va créer `MAX_THREADS` threads
  95. for (int i = 0; i < MAX_THREADS; i += 1) {
  96. thr_data[i].thread_id = i;
  97. // On passe un pointeur sur shared_counter
  98. thr_data[i].shared_counter = shared_counter;
  99. // Petit bug à la ligne suivante checkez vos erreurs de compilation
  100. int failure_reason = pthread_create(&threads[i], NULL, run, &thr_data[i]);
  101. if (failure_reason) {
  102. fprintf(stderr, "Error: pthread_create, rc: %d\n", failure_reason);
  103. return EXIT_FAILURE;
  104. }
  105. printf("Création du thread %i\n", i);
  106. }
  107.  
  108. // Joins threads, block until they finished their work.
  109.  
  110. // We create `MAX_THREADS` threads
  111. for (int i = 0; i < MAX_THREADS; i += 1) {
  112. pthread_join(threads[i], NULL);
  113. }
  114.  
  115. // We expect the counter to be equals to MAX_THREAD * 10000.
  116. // How can we correct? :) Indice: pthread_mutex_init, pthread_cond_init
  117. printf("Résultat du compteur partagé: %i\n", shared_counter_value(shared_counter));
  118.  
  119. printf("Les threads ont fini leur travail et son terminées le thread main peut maintenant se terminer\n");
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement