MarVin666

Threads SO 2 zad

Apr 11th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. volatile int licznik = 0;
  6.  
  7. void *iterate1() {
  8.         int random = rand() % 10000;
  9.         int i;
  10.         for(i = 0; i < random; i++) {
  11.                 printf("\n %d", i);
  12.                 licznik++;
  13.         }
  14. }
  15.  
  16. void main() {
  17.  
  18.         pthread_t thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9, thread10;
  19.         int iret1, iret2, iret3, iret4,iret5, iret6, iret7,iret8,iret9,iret10;
  20.         int iter = 100;
  21.  
  22.         iret1 = pthread_create( &thread1, NULL, iterate1, NULL);
  23.         iret2 = pthread_create( &thread2, NULL, iterate1, NULL);
  24.         iret3 = pthread_create( &thread3, NULL, iterate1, NULL);
  25.         iret4 = pthread_create( &thread4, NULL, iterate1, NULL);
  26.         iret5 = pthread_create( &thread5, NULL, iterate1, NULL);
  27.         iret6 = pthread_create( &thread6, NULL, iterate1, NULL);
  28.         iret7 = pthread_create( &thread7, NULL, iterate1, NULL);
  29.         iret8 = pthread_create( &thread8, NULL, iterate1, NULL);
  30.         iret9 = pthread_create( &thread9, NULL, iterate1, NULL);
  31.         iret10 = pthread_create( &thread10, NULL, iterate1, NULL);
  32.  
  33.  
  34.         pthread_join( thread1, NULL);
  35.         pthread_join( thread2, NULL);
  36.         pthread_join( thread3, NULL);
  37.         pthread_join( thread4, NULL);
  38.         pthread_join( thread5, NULL);
  39.         pthread_join( thread6, NULL);
  40.         pthread_join( thread7, NULL);
  41.         pthread_join( thread8, NULL);
  42.         pthread_join( thread9, NULL);
  43.         pthread_join( thread10, NULL);
  44.  
  45.         printf("\n Liczba wszystkich iteracji: %d \n", licznik);
  46.  
  47.         exit(EXIT_SUCCESS);
  48. }
  49. ~                                                                              
  50. ~
Add Comment
Please, Sign In to add comment