title: Getting segmentation fault while creating reader and writer threads inside a infinite while loop on random condition
body: <p>I am trying to create reader and writer threads (5 each) inside an infinite while loop randomly. The writers should wait until all readers are done executing. I am using mutex and condition variables for this purpose.</p>
  
  <pre><code>// Global variables
  int readerCount=0, writerCount=0, sharedVariable=1001;
  
  pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
  pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
  </code></pre>
  
  <p>The reader threads run the below function</p>
  
  <pre><code>void *readShared(void* param)
  {
      int shVariable, readers; //To copy and print global variables locally
  
      pthread_mutex_lock(&amp;m);
  
          readerCount++;
          while(readerCount &gt; 1)
          {
              pthread_cond_wait(&amp;read_cond, &amp;m);
          }
          readerCount--;
          readers = readerCount;
          shVariable = sharedVariable;
  
      pthread_mutex_unlock(&amp;m);
  
      printf("[Reader]:  Value in shared Variable = %d\n", shVariable);
      printf("Other \"Reader\" threads present = %d\n", readers);
  
      pthread_mutex_lock(&amp;m);
  
          if(readerCount &gt; 0)
          {
              pthread_cond_signal(&amp;read_cond);
          }
          else if(writerCount &gt; 0)
          {
              pthread_cond_signal(&amp;write_cond);
          }
  
      pthread_mutex_unlock(&amp;m);
  }
  </code></pre>
  
  <p>The writer threads run below function:</p>
  
  <pre><code>void *writeShared(void *valueToWrite)
  {
          //To copy and print global variables
      int value = *(int *)(valueToWrite), readers, shVariable;
  
      pthread_mutex_lock(&amp;m);
  
          writerCount++;
          while(readerCount &gt; 0 || writerCount &gt; 1)
          {
              pthread_cond_wait(&amp;write_cond, &amp;m);
          }
          sharedVariable = value;
          readers = readerCount;
          shVariable = sharedVariable;
          writerCount--;
  
      pthread_mutex_unlock(&amp;m);
  
      printf("[Writer]:  Value in shared Variable = %d\n", shVariable);
      printf("Value to write = %d\n", value);
      printf("Other \"Reader\" threads present = %d\n", readers);
  
  
      pthread_mutex_lock(&amp;m);
  
          if(readerCount &gt; 0)
          {
              pthread_cond_signal(&amp;read_cond);
          }
          else if(writerCount &gt; 0)
          {
              pthread_cond_signal(&amp;write_cond);
          }
  
      pthread_mutex_unlock(&amp;m);
  }
  </code></pre>
  
  <p>The main function:</p>
  
  <pre><code>int main()
  {
      pthread_t readers[5], writers[5];
      int readerIndex=0, writerIndex=0;
      while(1)
      {
          if(5 == readerIndex &amp;&amp; 5 == writerIndex)
          {
              break;
          }
          if(rand() % 2)
          {
              if(readerIndex &lt; 5)
              {
                  pthread_create(&amp;readers[readerIndex++], NULL, readShared, NULL);
              }
          }
          else
          {
              if(writerIndex &lt; 5)
              {
                  pthread_create(&amp;writers[writerIndex++], NULL, writeShared, (void*)((rand() % 1000) + 100));
              }
          }
      }
      for(int i=0; i&lt;5; i++)
      {
          pthread_join(readers[i], NULL);
          pthread_join(writers[i], NULL);
      }
      return 0;
  }
  </code></pre>
  
  <p>I tried running with gdb and obtained following:</p>
  
  <pre><code>Starting program: /home/abhijeet/threads/readWrite 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  
  [New Thread 0x7ffff77c4700 (LWP 3232)]
  [Reader]:  Value in shared Variable = 1001
  Other "Reader" threads present = 0
  
  [New Thread 0x7ffff6fc3700 (LWP 3233)]
  [Thread 0x7ffff77c4700 (LWP 3232) exited]
  
  [New Thread 0x7ffff67c2700 (LWP 3234)]
  [Reader]:  Value in shared Variable = 1001
  Other "Reader" threads present = 0
  
  [New Thread 0x7ffff5fc1700 (LWP 3235)]
  [Thread 0x7ffff67c2700 (LWP 3234) exited]
  
  Thread 3 "readWrite" received signal SIGSEGV, Segmentation fault.
  
  [Switching to Thread 0x7ffff6fc3700 (LWP 3233)]
  0x000055555555494a in writeShared (valueToWrite=0x36d) at PriorityReadWrite.c:13
  13      int value = *(int *)(valueToWrite), readers, shVariable;
  </code></pre>