Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include "gemiddelde.h"
  4.  
  5. float gem1, gem2, gem3, gem4;
  6.  
  7. void *bereken1(void *a);
  8. void *bereken2(void *a);
  9. void *bereken3(void *a);
  10. void *bereken4(void *a);
  11.  
  12. int main()
  13. {
  14.     float gemiddelde = 0;
  15.    
  16.     pthread_t tid1, tid2, tid3, tid4; /* identifiers voor de verschillende threads */
  17.     pthread_attr_t attr1, attr2, attr3, attr4; /* vzl thread attributen */
  18.    
  19.     pthread_attr_init(&attr1); /* zet de default attributen */
  20.     pthread_attr_init(&attr2); /* zet de default attributen */
  21.     pthread_attr_init(&attr3); /* zet de default attributen */
  22.     pthread_attr_init(&attr4); /* zet de default attributen */
  23.    
  24.    
  25.     pthread_create(&tid1, &attr1, bereken1, a1); /* maak een thread */
  26.     pthread_create(&tid2, &attr2, bereken2, a2); /* maak een thread */
  27.     pthread_create(&tid3, &attr3, bereken3, a3); /* maak een thread */
  28.     pthread_create(&tid4, &attr4, bereken4, a4); /* maak een thread */
  29.    
  30.     pthread_join(tid1, NULL); /* laat de main thread wachten op de andere threads */
  31.     pthread_join(tid2, NULL); /* laat de main thread wachten op de andere threads */
  32.     pthread_join(tid3, NULL); /* laat de main thread wachten op de andere threads */
  33.     pthread_join(tid4, NULL); /* laat de main thread wachten op de andere threads */
  34.    
  35.     gemiddelde = (gem1 + gem2 + gem3 + gem4) / 4;
  36.    
  37.     printf("Gemiddelde is %f\n", gemiddelde);
  38. }
  39.  
  40. void *bereken1(void *param)
  41. {
  42.     int i;
  43.     float som = 0;
  44.     float *a = (float*)param;
  45.     gem1 = 0;
  46.     for (i = 0; i < 100000; ++i)
  47.         som += a[i];
  48.     gem1 = som / 100000;
  49.     pthread_exit(0);
  50. }
  51.  
  52. void *bereken2(void *param)
  53. {
  54.     int i;
  55.     float som = 0;
  56.     float *a = (float*)param;
  57.     gem2 = 0;
  58.     for (i = 0; i < 100000; ++i)
  59.         som += a[i];
  60.     gem2 = som / 100000;
  61.     pthread_exit(0);
  62. }
  63.  
  64. void *bereken3(void *param)
  65. {
  66.     int i, som = 0;
  67.     float *a = (float*)param;
  68.     gem3 = 0;
  69.     for (i = 0; i < 100000; ++i)
  70.         som += a[i];
  71.     gem3 = som / 100000;
  72.     pthread_exit(0);
  73. }
  74.  
  75. void *bereken4(void *param)
  76. {
  77.     int i;
  78.     float som = 0;
  79.     float *a = (float*)param;
  80.     gem4 = 0;
  81.     for (i = 0; i < 100000; ++i)
  82.         som += a[i];
  83.     gem4 = som / 100000;
  84.     pthread_exit(0);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement