This is a common optimization in mutexes, designed to avoid wasting time when switching tasks, when the same thread can accept the mutex again. If the current thread still has time remaining in the time frame, you get more bandwidth in terms of user instructions executed per second, allowing it to accept the mutex rather than pause it, and switch to another thread (which probably causes a large reloading cache lines and various other delays).
If you have so much disputes over the mutex that this is a problem, then your application design is wrong. You have all these threads blocked on the mutex, and therefore do nothing: you probably better not need so many threads.
You must design your application so that if multiple threads compete for a mutex, it does not matter which thread gets the lock. Direct rivalry should also be a rare thing, especially direct rivalry with a lot of flows.
The only situation where I can think that this is an โOKโ scenario is where each thread expects a condition variable, which is then translated to wake them all up. Each thread will fight for the mutex, but if you do it right, everyone should quickly check that this is not a side track, and then release the mutex. Even then, this is called a โthunder-herdโ situation and is not ideal, precisely because it serializes all these streams.
source share