title: How do multiple mutex locks work?
body: <p>I am learning about POSIX threads and my professor has started teaching about the first readers-writers problem. This is the pseudocode I have about solving the problem (only for the first case: reader's preference).</p>
  
  <pre><code>semaphore rw_mutex = 1; /* semaphore common to both reader &amp; writer */
  semaphore mutex = 1; /* semaphore for reading (reader lock) */
  int read_count = 0; /* track number of readers in CS */
  
  
  Writer:
  do {
  lock(rw_mutex);
  /* ensure no writer or reader can enter */
  ...
  /* writing is performed */
  ...
  unlock(rw_mutex);
  /* release lock */
  } while (true);
  
  
  Reader:
  do
  {
  lock(mutex);
  /* first update read_count atomically */
  read_count++;
  if (read_count == 1) {
  lock(rw_mutex);
  /* ensure no writer can enter */
  }
  unlock(mutex);
  /* allow other readers to access */
  ...
  /* reading is performed */
  ...
  lock(mutex);
  read_count--;if (read_count == 0) unlock(rw_mutex);
  /* allow writers after
  last reader has left the CS */
  unlock(mutex);
  /* release lock */
  } while(true);
  </code></pre>
  
  <p>First of all this is my understanding of mutex locks: Once we create a lock and unlock pair, the code between these two entities can only be accessed by a single thread at a time. </p>
  
  <p>Now if my understanding is right, then I can pretty much understand what's happening in the Writer section of the above pseudocode. We are locking and then writing to the shared resource and in the meanwhile, no one can access the shared resource since it's locked and then we simply unlock it.</p>
  
  <p>But I have problems understanding the reader part. If we lock once, it means that it's locked for good until we unlock it again right? In that case, what's the use of locking twice in reader's section?</p>
  
  <p>My main question is this: 
  What does locking mean? and what's the difference between say lock(rw_mutex) and lock(mutex) in the above pseudocode? If once we call a lock, the program should lock it regardless of what parameter we pass in right? So what do these parameters: rw_mutex and mutex mean here? How does multiple mutex locking work?</p>