title: Modification to "Implementing an N process barrier using semaphores"
body: <p>Recently I see this problem which is pretty similar to First reader/writer problem.</p>
  
  <p><a href="https://stackoverflow.com/questions/6331301/implementing-an-n-process-barrier-using-semaphores">Implementing an N process barrier using semaphores</a></p>
  
  <p>I am trying to modify it to made sure that it can be reuse and work correctly.</p>
  
  <pre><code>n = the number of threads
  count = 0
  mutex = Semaphore(1)
  barrier = Semaphore(0)
  
  
  mutex.wait()
  count = count + 1
  if (count == n){ barrier.signal()}
  mutex.signal()
  
  barrier.wait()
  
  mutex.wait()
  count=count-1
  barrier.signal()
  if(count==0){ barrier.wait()}
  mutex.signal()
  </code></pre>
  
  <p>Is this correct? </p>
  
  <p>I'm wondering if there exist some mistakes I didn't detect.  </p>