Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define N 5
  6.  
  7. struct Matrix{
  8.     int mat[N][N];
  9.     int red;
  10.     int sum;
  11. };
  12.  
  13. void createMatrix(struct Matrix *data){
  14.     int i=0;
  15.     int j=0;
  16.     for(i=0;i<N;i++){
  17.         for(j=0;j<N;j++){
  18.             data->mat[i][j]=i+j;
  19.             printf("%d ", data->mat[i][j]);
  20.         }
  21.         printf(" \n");
  22.     }
  23. }
  24.  
  25. void *sumRed(void *data){
  26.     int i = 0;
  27.     struct Matrix *ndata = data;
  28.     int sumaPrev = ndata->sum;
  29.     for(i = 0; i<N;i++){
  30.         ndata->sum+=ndata->mat[ndata->red][i];
  31.     }
  32.     int newsum = ndata->sum;
  33.     printf("\n Suma %d red: %d \n",ndata->red, newsum-sumaPrev);
  34.    
  35. }
  36.  
  37.  
  38. int main() {
  39.     pthread_t threads[N];
  40.     int rc;
  41.     int i=0;
  42.     struct Matrix data;
  43.     createMatrix(&data);
  44.     data.sum = 0;
  45.  
  46.     for(i=0;i<N;i++){
  47.         data.red = i;
  48.         rc = pthread_create(&threads[i], NULL, sumRed, (void*)&data);
  49.         pthread_join(threads[i], NULL);
  50.         if (rc){
  51.             printf("ERROR; return code from pthread_create() is %d\n", rc);
  52.             exit(-1);
  53.         }
  54.     }
  55.  
  56.     printf("\nUkupna suma: %d \n", data.sum);
  57.     pthread_exit(NULL);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement