title: Multiple threads are not working simultaneously on Ubuntu in C
body: <p>I have written a code using <code>multi-threading</code> which creates two <code>threads</code>.</p>
  
  <ul>
  <li><code>Consumer Thread</code>: To <code>dequeue</code> one element from a <code>Circular Queue</code>.</li>
  <li><code>Producer Thread</code>: To <code>enqueue</code> ten elements in the <code>Circular Queue</code>.</li>
  </ul>
  
  <p><code>Consumer Thread</code> is in waiting state before insertion of six elements and only works i.e. <code>dequeue</code> element after <code>Producer Thread</code> inserts six elements in the <code>queue</code>.Following is my Code:</p>
  
  <pre><code>void *producer(void *t)
  {
    int i;
    long my_id = (long)t;
    for (i=0; i&lt;10; i++) 
    {
      pthread_mutex_lock(&amp;count_mutex);
      printf("\nInserting value = %d ",count);
      enQueue(count);
      displayQueue();
      count++;
      if (count == 6)
      {
        pthread_cond_signal(&amp;count_threshold_cv);
        printf("\nproducer: thread %ld, count = %d  Threshold reached.\n",my_id, count);
      }
      printf("\nproducer: thread %ld, count = %d, unlocking mutex\n",my_id, count);
      pthread_mutex_unlock(&amp;count_mutex);
    }
    pthread_exit(NULL);
  }
  
  void *consumer(void *t)
  {
    long my_id = (long)t;
    printf("Starting Consumer Thread(): thread %ld\n", my_id);
    pthread_mutex_lock(&amp;count_mutex);
    while (count&lt;6) 
    {
      pthread_cond_wait(&amp;count_threshold_cv, &amp;count_mutex);
      printf("\nconsumer: thread %ld Condition signal received.\n", my_id);
    }
    printf(" \nDeleted value =%d ",deQueue());
    displayQueue();
    printf("\nconsumer: thread %ld count now = %d.\n", my_id, count);
    pthread_mutex_unlock(&amp;count_mutex);
    pthread_exit(NULL);
  }
  
  int main (int argc, char *argv[])
  {
    int i, rc;
    long t1=1, t2=2;
    pthread_t threads[2];
    //Threads Created
  
    pthread_create(&amp;threads[1], &amp;attr, consumer, (void *)t1);
    pthread_create(&amp;threads[0], &amp;attr, producer, (void *)t2);
  
   //Thread Clean Up Followed
  }
  </code></pre>
  
  <p>The code is working as expected in <code>Windows(CodeBlocks)</code>.</p>
  
  <ul>
  <li>0-5 inserted in queue first</li>
  <li>On inserting 6, Threshold reached and one element removed from <code>queue</code>.</li>
  <li>After this, 6-9 inserted in <code>queue</code>.</li>
  </ul>
  
  <p>But when executed in <code>Ubuntu</code> the, First 0-9 elements are inserted then one element is removed from <code>queue</code>.
  Command for compiling in <code>ubuntu</code>: <code>g++ -pthread name.cpp</code>.
  What is the reason for this different behavior and how to fix this?
  For entire code <a href="http://ide.geeksforgeeks.org/FXJFqn" rel="nofollow noreferrer">Follow link</a></p>