Advertisement
Guest User

Untitled

a guest
Mar 25th, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | Source Code | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #define MAX_THREADS 5
  7.  
  8. static const int arr[5][5] = {{1, 2, 3, 4, 5},
  9.                               {6, 7, 8, 9, 10},
  10.                               {11, 12, 13, 14, 15},
  11.                               {16, 17, 18, 19, 20},
  12.                               {21, 22, 23, 24, 25}};
  13.  
  14. void* calculateRow(void* row);
  15. int main(int argc, char** argv)
  16. {
  17.     pthread_t threads[MAX_THREADS] = {0};
  18.  
  19.     for (int i = 0; i < MAX_THREADS; i++)
  20.     {
  21.         pthread_create(&threads[i], NULL, calculateRow, (void *)&i);
  22.     }
  23.     for (int i = 0; i < MAX_THREADS; i++)
  24.     {
  25.         pthread_join(threads[i], NULL);
  26.     }
  27.     return 0;
  28. }
  29.  
  30. void* calculateRow(void* row)
  31. {
  32.     int irow = (*(int *)row);
  33.     int sum = 0;
  34.     for (int i = 0; i < 5; i++)
  35.     {
  36.         sum += arr[irow][i];
  37.     }
  38.     printf("Sum of row (%d): %d\n", irow, sum);
  39.     pthread_exit(0);
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement