title: C multithreading : Race condition scenarios
body: <p>In the below code I'm just trying to see if I would be able to insert an element into an array , from each thread. It is working as expected. But then I wonder , under what circumstances there can be a race condition here. Do I really need volatile here, or semaphores? I tried removing semaphores and volatile keywords, still it works. I want to induce and see a race condition scenario as well here. On the same lines, Can I create a node from each thread and put all nodes into a linked list? These are all imaginary scenarios..!!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #include "small_appl.h"
  
  void * thread_func(void * arg);
  
  int itr=0;
  volatile int idx=0; //array index variable
  sem_t sem1;
  int arr_th[5];      //array where each thread will insert an element
  
  int func_pointed(int a,int num_t)
  {
      pthread_t arr_thr[num_t];
      int iter;
       //create threads
       for(iter=0;iter&lt;num_t;iter++)
       {
          pthread_create(&amp;arr_thr[iter],NULL,thread_func,(void *)a);
       }
  
       for (iter=0;iter&lt;num_t;iter++)
       {
           pthread_join(arr_thr[iter],NULL);
       }
  }
  
  int main(void)
  {
      int ip1=5,ip2=10,rev;
  
      rev=sem_init(&amp;sem1,0,0);
      s_type dev_s={
                      .f_ptr=func_pointed,
                      .str="Diwakar",
                      .val=5
                   };
  
      //initialize semaphore to 1
      sem_post(&amp;sem1);    
  
      func_aux(ip1,dev_s);
  
      for(rev=0;rev&lt;5;rev++)
      {
      printf("array : %d    ",arr_th[rev]);
      }
  
  }
  
  void * thread_func(void * arg)
  {
      sem_wait(&amp;sem1);
      printf("Got sema\n");
      arr_th[idx]=itr;
      idx++; itr++;
      printf("Releasing sema\n");
      sem_post(&amp;sem1);
  
      sleep(5);
  }
  </code></pre>