Do mutexes guarantee ordering of acquisition? : <p>A coworker had an issue recently that boiled down to what we believe was the following sequence of events in a C++ application with two threads:</p>

<ul>
<li><p>Thread A holds a mutex.</p></li>
<li><p>While thread A is holding the mutex, thread B attempts to lock it. Since it is held, thread B is suspended.</p></li>
<li><p>Thread A finishes the work that it was holding the mutex for, thus releasing the mutex.</p></li>
<li><p>Very shortly thereafter, thread A needs to touch a resource that is protected by the mutex, so it locks it again.</p></li>
<li><p>It appears that thread A is given the mutex again; thread B is still waiting, even though it "asked" for the lock first.</p></li>
</ul>

<p>Does this sequence of events fit with the semantics of, say, C++11's <code>std::mutex</code> and/or pthreads? I can honestly say I've never thought about this aspect of mutexes before.</p>
