title: C++ UNIX threading
body: <p>I am doing a project with threading in UNIX and C++.  Basically there is a producer thread, and 5 consumer threads.  The producer thread adds incrementing numbers into a queue at random times, and the consumer threads poll the q trying to remove it.  For some reason my q.size() keeps going negative and i cant figure out why.</p>
  
  <pre><code> #include &lt;queue&gt;
   #include &lt;list&gt;
  
   #include &lt;stdio.h&gt;
   #include &lt;unistd.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;string.h&gt;
   #include &lt;pthread.h&gt;
  
   using namespace std;
  
   #define NUM_CONSUMER_THREADS 5
   #define NUM_PRODUCER_THREADS 1
   #define BUFFER_SIZE 20
  
   void *c_thread_function(void *arg);
   void *p_thread_function(void *arg);
  
   queue&lt;int&gt; q;
  
   int produce(int cur)
   {
    int temp = cur + 1;
    return temp;
   }
  
   void append(int num)
   {
    if ( q.size() &lt; BUFFER_SIZE )
    {
     q.push(num);
    }
   }
  
   int take()
   {
    int removed = q.front();
    q.pop();
    sleep(1);
    return removed;
   }
  
   void consume(int num, int thread)
   {
    printf("%d consumed %d \n", thread, num);
   }
  
  
   int main() 
   {
    int result;
  
    pthread_t cthreads[NUM_CONSUMER_THREADS];
    pthread_t pthreads[NUM_PRODUCER_THREADS];
  
    void *thread_result;
  
    // build an array of consumer threads
    for(int num_of_cthreads = 0; num_of_cthreads &lt; NUM_CONSUMER_THREADS; num_of_cthreads++) 
    {
     result = pthread_create(&amp;(cthreads[num_of_cthreads]), NULL, c_thread_function, (void *)num_of_cthreads);
     if ( result != 0 )
     {
      perror( "Thread Creation Failed");
      exit(EXIT_FAILURE);
     }
     //sleep(1);  
    } 
  
    // build an array of producer threads
    for(int num_of_pthreads = 0; num_of_pthreads &lt; NUM_PRODUCER_THREADS; num_of_pthreads++) 
    {
     result = pthread_create(&amp;(pthreads[num_of_pthreads]), NULL, p_thread_function, NULL);
     if ( result != 0 )
     {
      perror( "Thread Creation Failed");
      exit(EXIT_FAILURE);
     }
     //sleep(1);  
    }
  
    printf("All threads created\n");
    while ( true )
    {
     // do nothing
    }
   }
  
   void *c_thread_function(void *arg)
   {
    int temp = (long)arg;
    printf("Consumer thread %d created \n", temp);
  
    while ( true )
    {
     while (  q.size() &gt; 0 )
     {
      int w = take();
      consume(w, temp);
      printf(" q size is now %d \n", q.size());
     }
    }
   }
  
   void *p_thread_function(void *arg) 
   {
    printf("Producer thread created \n");
  
    int itemsAdded = 0;
    int temp;
    int sleepTime;
  
    while ( true ) 
    {
     while ( q.size() &lt; BUFFER_SIZE )
     {
      temp = produce(itemsAdded);
  
      sleepTime = 1+(int)(9.0*rand()/(RAND_MAX+1.0));
      sleep(sleepTime);
  
      append(temp);
  
      printf("Producer adds: %d \n", temp);
      printf(" q size is now %d \n", q.size());
  
      itemsAdded++;
     }
    }
   }
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>Producer adds: 1 </p>
    
    <p>q size is now -1 </p>
    
    <p>0 consumed 1 </p>
    
    <p>q size is now -2 </p>
    
    <p>1 consumed 1 </p>
    
    <p>q size is now -3 </p>
    
    <p>3 consumed 1 </p>
    
    <p>q size is now -4 </p>
    
    <p>4 consumed 0 </p>
    
    <p>q size is now -5 </p>
    
    <p>0 consumed 0</p>
  </blockquote>