I have a program in C.
I created 3 threads with pthread_create, and I created a mutex to lock / unlock critical areas.
The 3rd argument to pthread_create is a pointer to a function executed by the thread.
In the examples found on the Internet, this function is always very simple , for example. prints a stream identifier or prints a message.
What happens when a function that a thread should execute contains a for loop ?
In my program, I would like each thread to work with a two-dimensional array.
Each thread must find the sum of the rows of the two-dimensional array. eg
Thread1 calculates the sum of the first row of a two-dimensional array
Thread2 will calculate the sum of the second row
Thread1 calculates the amount of the 3rd line
Thread3 calculates the sum of the 3rd row
I don't care about the flow order, but I need all the thread to select one of the lines.
I have the following code: sums two cells in a two-dimensional array.
Program:
creates NTHREADS
for(i=0; i < NTHREADS; i++) { pthread_create( &thread_id[i], NULL, CalculateSum, NULL ); }
Each thread waits for the rest to complete.
for(j=0; j < NTHREADS; j++) { pthread_join( thread_id[j], NULL); }
the function that each thread should perform, but for ONE row for the array and NOT for the array WHOLE
void *CalculateSum(void *dummyPtr) { pthread_mutex_lock( &mutex1 ); int i,j,sum = 0; for( i = 0; i <= N-1; i++) { for( j = 0; j <= M-1; j++) { sum = dimensional_array[i][j] + dimensional_array[i][j]; } printf(" Sum = %d\n", sum); } counter++; pthread_mutex_unlock( &mutex1 ); }
The whole program is as follows: The program has no compilation errors.
To start it, you must do: gcc -pthread program.c
//program.c #include <stdio.h> #include <pthread.h> #define NTHREADS 3 void *CalculateSum(void *); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; #define N 10 #define M 10 int dimensional_array[N][M]; main() { pthread_t thread_id[NTHREADS]; int i, j; for (i = 0; i <= N - 1; i++ ) for( j = 0; j <= M - 1; j++) dimensional_array[i][j] = i; for(i=0; i < NTHREADS; i++) { pthread_create( &thread_id[i], NULL, CalculateSum, NULL ); } for(j=0; j < NTHREADS; j++) { pthread_join( thread_id[j], NULL); } printf("Final counter value: %d\n", counter); //print ARRAY for (i = 0; i <= N-1; i++ ) { for( j = 0; j <= M-1; j++) printf("%d\t",dimensional_array[i][j]); printf("\n"); } } //Calculate void *CalculateSum(void *dummyPtr) { pthread_mutex_lock( &mutex1 ); int i,j,sum = 0; for( i = 0; i <= N-1; i++) { for( j = 0; j <= M-1; j++) { sum = dimensional_array[i][j] + dimensional_array[i][j]; } printf(" Sum = %d\n", sum); } counter++; pthread_mutex_unlock( &mutex1 ); }
So, I would like each thread to find the sum of the string, but I'm confused, I don't know how to do this.
In my program, every time a thread calls the Calculate function, the whole amount of lines is calculated, not just one
[Caution: for simplicity, I summarize the first element with its own, the point must understand how these flows can take place in this loop]
I would be glad if someone helped me
Thanks in advance