title: Circular buffer implementation in C testing issue
body: <p>I implemented my own circular buffer. I want to use two threads to test it. One thread continuously writing to the buffer while the other continuously reading it. When certain amount of data are read, print out the benchmark.</p>
  
  <p>The purpose of this circular buffer is write and read never access the same memory so there won't incur a race.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;time.h&gt;
  #include &lt;unistd.h&gt;
  
  #define MAX_NUM_ITEM 10000
  #define HASH_SIZE 32
  
  long write_count = 0;
  long read_count = 0;
  
  struct cBuf {
      int first;
      int last;
      int max_items;
      int item_size;
      int valid_items;
      unsigned char *buffer;
  };
  
  
  void init_cBuf(struct cBuf *buf, int max_items, int item_size) {
      buf -&gt; first = 0;
      buf -&gt; last = 0;
      buf -&gt; max_items = max_items;
      buf -&gt; item_size = item_size;
      buf -&gt; valid_items = 0;
      buf -&gt; buffer = calloc(max_items, item_size);
      return;
  }
  
  
  int isEmpty(struct cBuf *buf) {
      if (buf -&gt; valid_items == 0) {
          return 1;
      }
      else {
          return 0;
      }
  }
  
  int push(struct cBuf *buf, unsigned char *data) {
      if (buf -&gt; valid_items &gt;= buf -&gt; max_items) {
          // buffer full
          return -1;
      }
      else {
          // push data into the buffer
          memcpy(buf -&gt; buffer + (buf -&gt; last) * (buf -&gt; item_size), data, buf -&gt; item_size);
  
          // update cBuf info
          buf -&gt; valid_items++;
          buf -&gt; last = (buf -&gt; last + 1) % (buf -&gt; max_items);  
          return 0;
      }
  }
  
  int pop(struct cBuf *buf, unsigned char *new_buf) {
      if (isEmpty(buf)) {
          // buffer empty
          return -1;
      }
      else {
          // read data
          memcpy(new_buf, buf -&gt; buffer + (buf -&gt; first) * (buf -&gt; item_size), buf -&gt; item_size);
  
          // update cBuf info
          buf -&gt; first = (buf -&gt; first + 1) % (buf -&gt; max_items);
          buf -&gt; valid_items--;
          return 0;
      }
  }
  
  void *write_hash(void *ptr) {
      struct cBuf *buf = (struct cBuf *)(ptr);
  
      while (1) {
          unsigned char *hash = malloc(HASH_SIZE); // for simplicity I just create some data with 32-byte.
          if (push(buf, hash) == 0) {
              write_count++;
              //printf("put %lu items into the buffer. valid_items: %d\n", write_count, buf -&gt; valid_items);
          }
          free(hash);
          if (write_count == MAX_NUM_ITEM) {
              break;
          }
      }
  
      printf ("  thread id  = %lu\n", (long unsigned) (pthread_self ()));
      printf ("  total write = %lu\n\n", write_count);
      return NULL;
  }
  
  void *read_hash(void *ptr) {
  
      struct cBuf *buf = (struct cBuf *)(ptr);
      unsigned char *new_buf = malloc(HASH_SIZE);
      while (1) {
          if (pop(buf, new_buf) == 0) {
              read_count++;
              //printf("pop %lu items from the buffer. valid_items: %d\n", read_count, buf -&gt; valid_items);
          }
          if (read_count == MAX_NUM_ITEM) {
              break;
          }
      }
      free(new_buf);
  
      printf ("  thread id  = %lu\n", (long unsigned) (pthread_self ()));
      printf ("  total read = %lu\n\n", read_count);
  
  }
  
  int main(int argc, char const *argv[]) {
  
      struct cBuf buf;
      init_cBuf(&amp;buf, 200, HASH_SIZE);
  
      pthread_t write_thd, read_thd;
  
      double diff = 0.0, t1 = 0.0, t2 = 0.0;
      t1 = clock ();
  
      pthread_create(&amp;read_thd, NULL, read_hash, &amp;buf);
      pthread_create(&amp;write_thd, NULL, write_hash, &amp;buf);
  
      pthread_join(write_thd, NULL);
      pthread_join(read_thd, NULL);
  
  
      t2 = clock ();
      diff = (double)((t2 - t1) / CLOCKS_PER_SEC);
  
      printf ("----------------\nTotal time: %lf second\n", diff);
      printf ("Total write: %lu\n", write_count);
      printf ("write per-second: %lf\n\n", write_count / diff);
  
      return 0;
  }
  </code></pre>
  
  <p>It is very weird that the program never terminates if I left the "printf" in function "write_hash" and "read_hash" commented. If I uncommented these two "printf"s, the program will print out all push and pop info till the end and eventually the program exit successfully.</p>
  
  <p>Please help me to find if it's because something wrong with my circular buffer implementation, or somewhere else.</p>