title: How do you handle pthread_mutex_unlock failures?
body: <p>Assuming a thread successfully calls <code>pthread_mutex_lock</code>, is it still possible that a call to <code>pthread_mutex_unlock</code> in that same thread will fail? If so, can you actually do something about it besides abort the thread?</p>
  
  <pre><code>if(pthread_mutex_lock(&amp;m) == 0)
  {
     // got the lock, let's do some work
  
     if(pthread_mutex_unlock(&amp;m) != 0) // can this really fail?
     {
        // ok, we have a lock but can't unlock it?
     }
  }
  </code></pre>
  
  <p>From <a href="http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_mutex_unlock.html" rel="noreferrer">this page</a>, possible errors for <code>pthread_mutex_unlock()</code> are:</p>
  
  <blockquote>
    <p>[EINVAL]
        The value specified by mutex does not refer to an initialised
    mutex object.</p>
  </blockquote>
  
  <p>If the lock succeeded then this is unlikely to fail.</p>
  
  <blockquote>
    <p>[EAGAIN]
        The mutex could not be acquired because the maximum number of
    recursive locks for mutex has been exceeded.</p>
  </blockquote>
  
  <p>Really? For unlock?</p>
  
  <blockquote>
    <p>The pthread_mutex_unlock() function may fail if:</p>
    
    <p>[EPERM]
        The current thread does not own the mutex.</p>
  </blockquote>
  
  <p>Again, if lock succeeded then this also should not occur.</p>
  
  <p>So, my thoughts are if there is a successful lock then in this situation unlock should never fail making the error check and subsequent handling code pointless.</p>