title: What will happen if variable i is directly passed to a thread in this code?
body: <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #define NO_OF_THREADS 5
  
  void print_function(int* i)
  {
   int count;
   for(count=0; count&lt;10; count++)
    printf("Hello world from thread %d\n",*i);
   pthread_exit(NULL);
  }
  
  int main()
  {
   pthread_t printThreads[NO_OF_THREADS];
   int thread_no[NO_OF_THREADS];
   int i;
  
   for(i=0; i&lt;NO_OF_THREADS; i++)
   {
    thread_no[i] = i;
    pthread_create(&amp;printThreads[i], NULL, print_function,&amp;thread_no[i]);
   }
  
   int j;
   for(j=0;j&lt;NO_OF_THREADS;j++)
    pthread_join(printThreads[j],NULL);
   puts("Main over and out");
   return 0;
  }
  </code></pre>
  
  <p>Without just passing the address of the counter variable <code>i</code> directly as an argument
  for the <code>pthread_create</code> function, it is first assigned to an unused slot of an array
  and then the address of the array element is passed as the argument. This is done
  to avoid a race condition accessing <code>i</code> between parent thread and created threads.
  Explain what will happen if variable <code>i</code> is directly passed to a thread.</p>