Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define MAX_THREADS 5
- static const int arr[5][5] = {{1, 2, 3, 4, 5},
- {6, 7, 8, 9, 10},
- {11, 12, 13, 14, 15},
- {16, 17, 18, 19, 20},
- {21, 22, 23, 24, 25}};
- void* calculateRow(void* row);
- int main(int argc, char** argv)
- {
- pthread_t threads[MAX_THREADS] = {0};
- for (int i = 0; i < MAX_THREADS; i++)
- {
- pthread_create(&threads[i], NULL, calculateRow, (void *)&i);
- }
- for (int i = 0; i < MAX_THREADS; i++)
- {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
- void* calculateRow(void* row)
- {
- int irow = (*(int *)row);
- int sum = 0;
- for (int i = 0; i < 5; i++)
- {
- sum += arr[irow][i];
- }
- printf("Sum of row (%d): %d\n", irow, sum);
- pthread_exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement