title: How to specify the number of threads I want to use?
body: <p>I am trying to Multiply the matrix with use of threads. The code seems to be working. I just need to know how to specify the number of threads so that I can count how much time it's taking. I want to create a table of threads and time it's taking to compute the matrix.
  I am dynamically allocating the matrix and filling it with random numbers.
  I am creating threads to compute the resultant matrix.</p>
  <p>C Program to multiply two matrix using pthreads without use of global variables</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;pthread.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;stdlib.h&gt;
  
  
  
  //Each thread computes single element in the resultant matrix
  void *mult(void* arg)
  {
      int *data = (int *)arg;
      int k = 0, i = 0;
      
      int x = data[0];
      for (i = 1; i &lt;= x; i++)
          k += data[i]*data[i+x];
      
      int *p = (int*)malloc(sizeof(int));
          *p = k;
      
  //Used to terminate a thread and the return value is passed as a pointer
      pthread_exit(p);
  }
  
  //Driver code
  int main()
  {
  
      int i, j, k, row1, col1, row2, col2, r, sum;
  
      printf(&quot;Enter the number of rows for matrix 1\n&quot;);
      scanf(&quot;%d&quot;,&amp;row1);
      printf(&quot;Enter the number of columns for matrix 1 \n&quot;);
      scanf(&quot;%d&quot;,&amp;col1);
      printf(&quot;Enter the number of rows for matrix 2 \n&quot;);
      scanf(&quot;%d&quot;,&amp;row2);
      printf(&quot;Enter the number of columns for matrix 2\n&quot;);
      scanf(&quot;%d&quot;,&amp;col2);
  
  
      int **a = (int **) malloc(row1 * sizeof(int *));
      for(i=0;i&lt;row1;i++)
          a[i] = (int *) malloc(col1 * sizeof(int));
  
      int **b = (int **) malloc(row2 * sizeof(int *));
      for(i=0;i&lt;row2;i++)
          b[i] = (int *) malloc(col2 * sizeof(int));
  
      
          
      for(i=0;i&lt;row1;i++)
      {
          for(j=0;j&lt;col1;j++)
          {
              a[i][j] = (rand()%9) + 1;
          }
      }
  
      for(i=0;i&lt;row2;i++)
      {
          for(j=0;j&lt;col2;j++)
          {
              b[i][j] = (rand()%9) +1;
          }
      }
      
      int N = row1*col2;
      
      
      //declaring array of threads of size row1*col2  
      pthread_t *threads;
      threads = (pthread_t*)malloc(N*sizeof(pthread_t));
      
      int count = 0;
      int* data = NULL;
      for (i = 0; i &lt; row1; i++)
          for (j = 0; j &lt; col2; j++)
              {
                  
              //storing row and column elements in data
              data = (int *)malloc((N)*sizeof(int));
              data[0] = col1;
      
              for (k = 0; k &lt; col1; k++)
                  data[k+1] = a[i][k];
      
              for (k = 0; k &lt; row2; k++)
                  data[k+col1+1] = b[k][j];
              
              //creating threads
                  pthread_create(&amp;threads[count++], NULL,
                              mult, (void*)(data));
                  
                      }
      
      printf(&quot;RESULTANT MATRIX IS :- \n&quot;);
      for (i = 0; i &lt; N; i++)
      {
      void *k;
      
      //Joining all threads and collecting return value
      pthread_join(threads[i], &amp;k);
              
          
          int *p = (int *)k;
      printf(&quot;%d &quot;,*p);
      if ((i + 1) % col2 == 0)
          printf(&quot;\n&quot;);
      }
  
      
  
  return 0;
  }
  </code></pre>