Mutexes are faster than Spinlocks
This comes around every so often, and it isn&#x27;t very interesting in that the best mutexes basically spins 1 or a couple times then falls back to a lock. It isn&#x27;t a true pure spinlock vs pure lock (mutex&#x2F;futex) fight.<p>I think the linux futex can be implemented through the VDSO (can somebody correct me on this), so that eliminates the worse of the sycall costs.<p>His benchmark is weird, but maybe I&#x27;m reading it wrong:<p>* Instead of reducing thread count to reduce contention he appears to increase the number of locks available. This is still a bad scenario for spinlocks since they will still have bad interactions with scheduler (they will use a large amount of cpu time when and get evicted from the run queue and need to be rescheduled).<p>* Also, I didn&#x27;t see him pin any of the threads, so all those threads will start sharing some cpus since the OS does need to be doing some work on a them too.<p>* And Rust can be a little hard to read, but it seem he packed his locks on the same cache line? I don&#x27;t see any padding in his AmdSpinlock struct. That would be a huge blow for the spinlock because of the false sharing issues. He&#x27;s getting all the cache coherence traffic still because of  it.<p>The worst cases for the spinlock are not understanding scheduling costs and the cache thrashing that can occur.<p>What they call the AMD spinlock (basically just a regular sane spinlock that tries to prevent cache thrashing) has its best performace with a low number of threads, assigned to different cores under the same L3 segment.<p>(Does anybody know if AMD&#x27;s new microarchitecture went away from the MOESI&#x2F;directory based cache towards Intel&#x27;s MESIF&#x2F;snoop model?)<p>The MOESI model might have performed better in this regard under worse case scenario since it doesn&#x27;t need to write the cache line back and can just forward around the dirty line as it keeps track of who owns it.<p>And if you run under an MESIF-based cache and you can keep your traffic local to your L3 segment, you are backstopped there and never need to go anywhere else.<p>A spinlock is a performance optimization and should be treated as one. You need to have intimate knowledge of the architecture you are running under and the resources being used.<p>(edit: answered my own question, apparently the vdso is still pretty limited in what it exports, so no. it isn&#x27;t happening at this time from what i can tell.)