How does the other thread know that the mutex is released?

spin locks are easy to understand, which is constantly polling, so what is the mechanism of mutexes? What are the drawbacks of this mechanism that make it better to use spin locks in some situations?


does not mean that the thread "knows", but enters a blocking state while waiting, and the thread that owns the lock needs to notify the waiting thread when it releases the lock so that the waiting thread can be awakened.

as for the spin lock, its advantage is that it does not need to enter the blocking state, thus saving the switching overhead of blocking and waking up. But the disadvantage is that it will take up CPU, all the time. If you can't get the lock for a long time, it will increase the overhead and degrade the performance.


add that upstairs, the overhead of thread switching is greater than that of short polling. If the waiting time outside the critical area is very short, polling is used to reduce the overhead. In this case, the use of ordinary mutexes will lead to frequent thread switching. If you need to wait outside the critical zone for a long time, using a normal mutex will cost less than polling, and the thread does not have to "wait" to go to the "Rest". When someone comes out of the critical zone, the Rest thread will be woken up. For more information, please refer to the operating system. Try to read the original English version. The translation of the Chinese version is very poor, poorly translated

.
Menu