Originally posted by Zan Lynx
View Post
Also, with a proper mutex, it's theoretically possible for the scheduler to elevate the priority of the thread holding the lock since it now knows that a lot of other threads are waiting for it.
The specifics of how the scheduler handles this are somewhat beyond me, but the main point is that the scheduler has ultimate control over which threads are running, and by using mutexes, you're giving it more information so that it can make more intelligent decisions.
All that said, there are probably some scenarios where it statistically makes sense to spin rather than use a mutex - likely cases where the probability of completing the critical region within your time slice is very high, which implies extremely small critical regions. Still though, you run the risk of massive latency spikes if the thread holding the spin lock happens to be scheduled out at that very instant, which appears to be what's happening in the blog post.
This behavior isn't really surprising, and I'm not sure how the kernel could handle it any better without adding something like a fine-grained API to provide scheduling hints to the kernel.
Originally posted by Zan Lynx
View Post
Comment