When I implemented the C++14 type std::shared_timed_mutex (or std::shared_mutex as it was originally called) I must have been half asleep, because I completely messed up the timed locking functions, try_lock_for, try_lock_until, try_shared_lock_for and try_shared_lock_until. I wrote them to only use the timeout when trying to lock the internal mutex object, but that is not the actual lock, it just protects access to shared_timed_mutex::_M_state, so it is only held for very short periods and is usually not heavily contended. That means the internal lock gets taken immediately, but then if the actual reader or writer lock can't be taken the functions return immediately, instead of retrying for the specified time. This fixes those functions to block on the condition variables until the specified time, and adds a test to confirm that the timed functions keep trying instead of returning immediately. I've also cleaned the code up considerably, documenting the algorithm, replacing explicit loops around condition variable waits with the form of waiting that takes a predicate, and replacing lots of fiddly bit twiddling with much simpler operations. Tested x86_64-linux, powerpc64le-linux and x86_64-dragonfly3.6 with hacked config to test the code changed in this patch as well as the default pthread_rwlock_t implementation. This only affects C++14, so I'd like to commit it to trunk and the 4.9 branch.