4 ms·
> If you want your spinlock to be hella fast on contention just make sure you `sched_yield` before each retry Shouldn't you rather use pause instructions (_mm_
by spacechild1 2y ago
> If you want your spinlock to be hella fast on contention just make sure you `sched_yield` before each retry
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?
- xxs 2y agostart with few attempts w/o pause, just busy CAS, then CAS + pause, then yield (and backoff/sleep with random duration)
- senderista 2y agoNo, 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).
- xxs 2y agoof course, there is very rarely any a naked CAS