How can I have multiple threads in C working on the same loop for a two-dimensional array?

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

+4
source share
2 answers

You must create an array of parameters for the stream and pass them into the streams one by one. In your case, a single pointer to int is enough: you pass your threadindex index from zero to NTHREADS to the NTHREADS , and the thread passes back the sum for the strings in such a way that row % NTHREADS == threadindex .

Here's what your stream function looks like:

 void *CalculateSum(void *args) { int *argPtr = args; int i,j,sum = 0; int threadindex = *argPtr; for( i = 0; i <= N-1; i++) { if (i % NTHREADS != threadindex) continue; for( j = 0; j <= M-1; j++) { sum += dimensional_array[i][j]; } } pthread_mutex_lock( &mutex1 ); Mutex must go here counter++; pthread_mutex_unlock( &mutex1 ); // Pass the value back: *argPtr = sum; } main() { pthread_t thread_id[NTHREADS]; int thread_args[NTHREADS]; int i, j; pthread_mutex_init(&mutex1, NULL); 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++) { thread_args[i] = i; pthread_create( &thread_id[i], NULL, CalculateSum, &thread_args[i]); } int sum = 0; for(j=0; j < NTHREADS; j++) { pthread_join( thread_id[j], NULL); sum += thread_args[i]; } printf("Final counter value: %d. Total: %d\n", counter, sum); } 
+5
source

To calculate the sum of one row (ignoring the flow material):

 void *CalculateSum(void *dummyPtr) { int j,sum = 0; int i = (int)dummyPtr; for( j = 0; j <= M-1; j++) { sum += dimensional_array[i][j]; } printf(" Sum = %d\n", sum); pthread_mutex_lock( &mutex1 ); counter++; pthread_mutex_unlock( &mutex1 ); } 

Then create the stream as follows:

 int line_number = 2; // Or whatever line to print`enter code here` pthread_create( &thread_id[i], NULL, CalculateSum, (void *)line_number ); 

EDIT: return "counter ++".

+3
source

Source: https://habr.com/ru/post/1446518/


All Articles