title: POSIX Threads: Condition Variables - what's the point?
body: <p>I've been working with pthreads a fair bit recently and there's one little thing I still don't quite get. I know that condition variables are designed to wait for a specific condition to come true (or be 'signalled'). My question is, how does this differ at all from normal mutexes?</p>
  
  <p>From what I understand, aren't condition variables just a mutex with additional logic to unlock <em>another</em> mutex (and lock it again) when the condition becomes true?</p>
  
  <p>Psuedocode example:</p>
  
  <pre><code>mutex mymutex;
  condvar mycond;
  int somevalue = 0;
  
  onethread()
  {
      lock(mymutex);
  
      while(somevalue == 0)
          cond_wait(mycond, mymutex);
  
      if(somevalue == 0xdeadbeef)
          some_func()
  
      unlock(mymutex);
  }
  
  otherthread()
  {
      lock(mymutex);
  
      somevalue = 0xdeadbeef;
  
      cond_signal(mycond);
  
      unlock(mymutex);
  }
  </code></pre>
  
  <p>So cond_wait in this example unlocks <code>mymutex</code>, and then waits for <code>mycond</code> to be signalled.</p>
  
  <p>If this is so, aren't condition variables just mutexes with extra magic? Or do I have a misunderstanding of the fundamental basics of mutexes and condition variables?</p>