title: Reader/Writer implementation in C
body: <p>I'm currently learning about concurrency at my University. In this context I have to implement the reader/writer problem in C, and I think I'm on the right track.</p>
  
  <p>My thought on the problem is, that we need two locks <code>rd_lock</code> and <code>wr_lock</code>. When a writer thread wants to change our global variable, it tries to grab both locks, writes to the global and unlocks. When a reader wants to read the global, it checks if <code>wr_lock</code> is currently locked, and then reads the value, however one of the reader threads should grab the <code>rd_lock</code>, but the other readers should not care if <code>rd_lock</code> is locked.</p>
  
  <p>I am not allowed to use the implementation already in the pthread library.</p>
  
  <pre class="lang-c prettyprint-override"><code>typedef struct counter_st {
      int value;
  } counter_t;
  
  counter_t * counter;
  pthread_t * threads;
  
  int readers_tnum;
  int writers_tnum;
  
  pthread_mutex_t rd_lock;
  pthread_mutex_t wr_lock;
  
  void * reader_thread() {
  
      while(true) {
          pthread_mutex_lock(&amp;rd_lock);
          pthread_mutex_trylock(&amp;wr_lock);
          int value = counter-&gt;value;
          printf("%d\n", value);
          pthread_mutex_unlock(&amp;rd_lock);
      }
  }
  
  void * writer_thread() {
  
      while(true) {
          pthread_mutex_lock(&amp;wr_lock);
          pthread_mutex_lock(&amp;rd_lock);
  
          // TODO: increment value of counter-&gt;value here.
          counter-&gt;value += 1;
  
          pthread_mutex_unlock(&amp;rd_lock);
          pthread_mutex_unlock(&amp;wr_lock);
      }
  }
  
  int main(int argc, char **args) {
  
      readers_tnum = atoi(args[1]);
      writers_tnum = atoi(args[2]);
  
      pthread_mutex_init(&amp;rd_lock, 0);
      pthread_mutex_init(&amp;wr_lock, 0);
  
      // Initialize our global variable
      counter = malloc(sizeof(counter_t));
      counter-&gt;value = 0;
  
      pthread_t * threads = malloc((readers_tnum + writers_tnum) * sizeof(pthread_t));
      int started_threads = 0;
  
      // Spawn reader threads
      for(int i = 0; i &lt; readers_tnum; i++) {
          int code = pthread_create(&amp;threads[started_threads], NULL, reader_thread, NULL);
  
          if (code != 0) {
              printf("Could not spawn a thread.");
              exit(-1);
          } else {
              started_threads++;
          }
      }
  
      // Spawn writer threads
      for(int i = 0; i &lt; writers_tnum; i++) {
          int code = pthread_create(&amp;threads[started_threads], NULL, writer_thread, NULL);
  
          if (code != 0) {
              printf("Could not spawn a thread.");
              exit(-1);
          } else {
              started_threads++;
          }
      }
  }
  </code></pre>
  
  <p>Currently it just prints a lot of zeroes, when run with 1 reader and 1 writer, which means, that it never actually executes the code in the writer thread. I know that this is not going to work as intended with multiple readers, however I don't understand what is wrong, when running it with one of each.</p>