title: Reader Writer with a monitor
body: <p>Im using the pthreads library to try and implement a reader/writer application with a monitor.</p>
  
  <p>I call my thread functions in the main the following way</p>
  
  <pre><code>pthread_t *tid;
  tid = (pthread_t*)malloc(sizeof(pthread_t)*(r + w));
  int addr = 0;
  //Create r # readers
  for (int a = 0; a &lt; r; a++)
  {
      pthread_create(&amp;tid[addr], NULL, Reader, (void*)&amp;a);
      addr++;
  }
  </code></pre>
  
  <p>and then they complete their function through the following 3 function calls</p>
  
  <pre><code>void *Reader(void *pid)
  {
      static int *id = (int*)pid;
      for (int i = 0; i &lt; 10; i++)
      {
          beginRead(id);
          endRead();
          #ifdef __unix
              usleep(R);
          #endif
          #ifdef _WIN32
              Sleep(R);
          #endif
      }
      return 0;
  }
  
  void beginRead(int *id)
  {
      //If writelock or writer_queued then wait(canRead)
      pthread_mutex_lock(&amp;writelock);
      while (writer_queued)
      {
          reader_queued = true;
          pthread_cond_wait(&amp;canRead, &amp;writelock);
          reader_queued = false;
      }
      readers++;
      pthread_mutex_unlock(&amp;writelock);
      pthread_mutex_lock(&amp;output);
      time_t t = time(0);
      struct tm * now = localtime(&amp;t);
      fprintf(outputfile, "DB value read =:%d:%d by reader number: %d\n", now-        &gt;tm_sec, (now-&gt;tm_sec) / 1000, (int)*id);
      printf("DB value read =:%d:%d by reader number: %d\n", now-&gt;tm_sec, (now-&gt;tm_sec)/1000, (int)*id);
      pthread_mutex_unlock(&amp;output);
      pthread_cond_broadcast(&amp;canRead);
  }
  
  void endRead()
  {
      --readers; // just finished a reader
      if (readers == 0)
      {
          pthread_cond_signal(&amp;canWrite);
      }
  }
  </code></pre>
  
  <p>This is supposed to make each thread go into the critical section of the code 10 times(so if there are 2 threads created it would access the critical section 20 times)I get the threads to access the critical section of my code the correct number of times, i just can't get it to print the correct thread ID#</p>
  
  <p>so if i called this function with r=2 and R=200 my output would be:</p>
  
  <pre><code>DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  </code></pre>
  
  <p>when it should be something along the lines of</p>
  
  <pre><code>DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number:1
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  DB value read =:30:0 by reader number: 2
  </code></pre>
  
  <p>depending on the timing of each thread accessing the critical section</p>