start with few attempts w/o pause, just busy CAS, then CAS + pause, then yield (and backoff/sleep with random duration)
Re:The Fastest Mutexes
by senderista on Thursday October 03, 2024 @02:25PM
(#41733410)
No, you shouldn’t be calling CAS in a tight loop, but rather a relaxed load to check if a CAS might be successful, with PAUSE equivalent after each load. After spinning for ~context switch latency you should back off with sched_yield before retrying the spin loop (and eventually start sleeping on backoff with exponential jitter etc).
Shouldn't you rather use pause instructions (_mm_pause, __yield, ISB, etc.) instead of thread switching? That's what I have seen in most spin lock implementations. Maybe it depends on the use case?