title: How can i have multiple threads in C working on the same for loop of a two-dimensional array?
body: <p>i have a program in C.</p>
  
  <p>I have created 3 threads with pthread_create and i have created a mutex
  in order to lock/unlock the critical regions.</p>
  
  <p>The <strong>3nd argument</strong> of <strong>pthread_create</strong> is a pointer to a function that the thread will execute.</p>
  
  <p>In the examples i've found in the Web, this function is  <strong>always very simple</strong> e.g. prints the thread id or prints a message.</p>
  
  <p>What happens when the function that the thread shall execute contains a <strong>for loop?</strong></p>
  
  <p>Cause in my program i would like each one of the threads to work with a two dimensional array.</p>
  
  <p>Each thread shall find the sum of a line of a two-dimensional array.
  e.g. </p>
  
  <p><strong>Thread1</strong> shall calculate the sum of first line of the 2-dimensional array</p>
  
  <p><strong>Thread2</strong> shall calculate the sum of the second line<br>
  <strong>Thread1</strong> shall calculate the sum of the 3nd line<br>
  <strong>Thread3</strong> shall calculate the sum of the 3nd line</p>
  
  <p>I don't care about the order of the threads, but i need every
  thread to pick one of the lines.</p>
  
  <p>I have the following code that <strong>sums</strong> two cells in the two dimensional array.</p>
  
  <p>The program:</p>
  
  <ol>
  <li><p><strong>creates NTHREADS</strong></p>
  
  <pre><code> for(i=0; i &lt; NTHREADS; i++)
      {
         pthread_create( &amp;thread_id[i], NULL, CalculateSum, NULL );
      }
  </code></pre></li>
  <li><p><strong>Each thread waits for the others to finish</strong></p>
  
  <pre><code>for(j=0; j &lt; NTHREADS; j++)
     {
        pthread_join( thread_id[j], NULL);
     }
  </code></pre></li>
  <li><p>the function that each thread shall execute but for <strong>ONE line</strong> of the array and <strong>NOT</strong> for the WHOLE array is </p>
  
  <pre><code>void *CalculateSum(void *dummyPtr)
  {
     pthread_mutex_lock( &amp;mutex1 );
  
   int i,j,sum = 0;
  
    for( i = 0; i &lt;= N-1; i++) {
          for( j = 0; j &lt;= M-1; j++) {
                  sum = dimensional_array[i][j] + dimensional_array[i][j];
          }
           printf(" Sum = %d\n", sum);
          }
  
     counter++;
  
     pthread_mutex_unlock( &amp;mutex1 );
  }
  </code></pre></li>
  </ol>
  
  <p>The whole program is as follows:
  The program does not have any compilation error.</p>
  
  <p>In order to run it you shall do: <strong>gcc -pthread program.c</strong></p>
  
  <pre><code>    //program.c
  
     #include &lt;stdio.h&gt;
     #include &lt;pthread.h&gt;
  
     #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 &lt;= N - 1; i++ )
             for( j = 0; j &lt;= M - 1; j++)
                     dimensional_array[i][j] = i;
  
        for(i=0; i &lt; NTHREADS; i++)
        {
           pthread_create( &amp;thread_id[i], NULL, CalculateSum, NULL );
        }
  
        for(j=0; j &lt; NTHREADS; j++)
        {
           pthread_join( thread_id[j], NULL);
        }
  
  
  
        printf("Final counter value: %d\n", counter);
  
        //print ARRAY
        for (i = 0; i &lt;= N-1; i++ ) {
             for( j = 0; j &lt;= M-1; j++)
                     printf("%d\t",dimensional_array[i][j]);
             printf("\n");
             }
     }
     //Calculate
     void *CalculateSum(void *dummyPtr)
     {
        pthread_mutex_lock( &amp;mutex1 );
  
      int i,j,sum = 0;
  
       for( i = 0; i &lt;= N-1; i++) {
             for( j = 0; j &lt;= M-1; j++) {
                     sum = dimensional_array[i][j] + dimensional_array[i][j];
             }
              printf(" Sum = %d\n", sum);
             }
  
        counter++;
  
        pthread_mutex_unlock( &amp;mutex1 );
     }
  </code></pre>
  
  <p>So, i would like each thread to find the sum of a line but i'm confused, i don't know
  how to do that.</p>
  
  <p>In my program every time a thread calls the Calculate function, all the sum of the lines are computed and not just one</p>
  
  <p>[Caution:For simplicity i sum the first element with it's own,the point is to understand
  how those threads can all take place in that for loop]</p>
  
  <p>I would be glad if someone could help me</p>
  
  <p>Thanks, in advance</p>