title: Consumer/Producer with pthreads having waiting times
body: <p>I am trying to implement a slightly modified version of Consumer/Producer program with a code i I picked on the internet. It is as follows with my own modifications:</p>
  
  <pre><code>   /*
   *  Solution to Producer Consumer Problem
   *  Using Ptheads, a mutex and condition variables
   *  From Tanenbaum, Modern Operating Systems, 3rd Ed.
   */
  
  /*
   In this version the buffer is a single number.
   The producer is putting numbers into the shared buffer
   (in this case sequentially)
   And the consumer is taking them out.
   If the buffer contains zero, that indicates that the buffer is empty.
   Any other value is valid.
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  #define MAX 3       /* # of item  to produce */
  pthread_mutex_t the_mutex;
  pthread_cond_t condc, condp;
  int consumeTimes[MAX] = { 1, 4, 3 };
  int toConsume = 0;
  
  void* producer(void *ptr) {
      int i;
  
      for (i = 0; i &lt; MAX; i++) {
          pthread_mutex_lock(&amp;the_mutex); /* protect buffer */
          /*while (buffer != 0)              /* If there is something in the buffer then wait
           pthread_cond_wait(&amp;condp, &amp;the_mutex);*/
          printf("Producer: Produced item %d \n", i);
          toConsume++;
          pthread_cond_signal(&amp;condc); /* wake up consumer */
          pthread_mutex_unlock(&amp;the_mutex); /* release the buffer */
          sleep(3);
  
      }
      pthread_exit(0);
  }
  
  void* consumer(void *ptr) {
      int i;
      for (i = 0; i &lt; MAX; i++) {
          pthread_mutex_lock(&amp;the_mutex); /* protect buffer */
          while (toConsume &lt;= 0) /* If there is nothing in the buffer then wait */
              pthread_cond_wait(&amp;condc, &amp;the_mutex);
          sleep(consumeTimes[i]);
          printf("Consumer: Consumed item %d\n", i);
          toConsume--;
          //pthread_cond_signal(&amp;condp);  /* wake up consumer */
          pthread_mutex_unlock(&amp;the_mutex); /* release the buffer */
  
      }
      pthread_exit(0);
  }
  
  int main(int argc, char **argv) {
      pthread_t pro, con;
  
      // Initialize the mutex and condition variables
      /* What's the NULL for ??? */
      pthread_mutex_init(&amp;the_mutex, NULL);
      pthread_cond_init(&amp;condc, NULL); /* Initialize consumer condition variable */
      pthread_cond_init(&amp;condp, NULL); /* Initialize producer condition variable */
  
      // Create the threads
      pthread_create(&amp;con, NULL, consumer, NULL);
      pthread_create(&amp;pro, NULL, producer, NULL);
  
      // Wait for the threads to finish
      // Otherwise main might run to the end
      // and kill the entire process when it exits.
      pthread_join(&amp;con, NULL);
      pthread_join(&amp;pro, NULL);
  
      // Cleanup -- would happen automatically at end of program
      pthread_mutex_destroy(&amp;the_mutex); /* Free up the_mutex */
      pthread_cond_destroy(&amp;condc); /* Free up consumer condition variable */
      pthread_cond_destroy(&amp;condp); /* Free up producer condition variable */
  
  }
  </code></pre>
  
  <p>Here's what I want to do:</p>
  
  <p>Basically, each item has a consume and a production time.</p>
  
  <p>The consume times are indicated for each item in the <strong>consumeTimes array</strong>.</p>
  
  <p>So, for example <strong>consumeTimes[0] = 1</strong> which means that consuming the 1st item should take only a single time unit.</p>
  
  <p>For production, I use a constant time value, <strong>sleep(3)</strong>, so producing every item should take 3 time units.</p>
  
  <p>When I run the code, I get the following output all the time:</p>
  
  <pre><code>Producer: Produced item 0 
  Consumer: Consumed item 0
  Producer: Produced item 1 
  Consumer: Consumed item 1
  Producer: Produced item 2 
  Consumer: Consumed item 2
  </code></pre>
  
  <p>However, considering the production and consume times it should be like this:</p>
  
  <pre><code>Producer: Produced item 0 (t=0)
  Consumer: Consumed item 0 (t=1)
  Producer: Produced item 1 (t=3)
  Producer: Produced item 2 (t=6)
  Consumer: Consumed item 1 (t=7)
  Consumer: Consumed item 2 (t=9)
  </code></pre>
  
  <p>In short, producer must always produce a new item in every 3 time intervals. But in this case, it seems to be waiting on the consumer to finish and I can't seem to figure out why.</p>