3 ms·
The 25 ns is from "Latency numbers every programmer should know" [0], and the 2017 edition puts it at 17 ns. That's essentially the cost of an atomic operation
by exDM69 9y ago
The 25 ns is from "Latency numbers every programmer should know" [0], and the 2017 edition puts it at 17 ns.
That's essentially the cost of an atomic operation in the CPU interconnect.
A futex is a way to implement a mutex so that the uncontended case is just a spinlock acquire, and the contended case falls back to calling the kernel.
[0] https://people.eecs.berkeley.edu/~rcs/research/interactive_latency.html https://people.eecs.berkeley.edu/~rcs/research/interactive_l...
- bogomipz 9y agoThanks, why do you need to spin if there is no contention for the lock though? That sounds counterintuitive.
- slrz 9y agoWell, you don't. Just like when acquiring an unlocked spinlock. ;-) Should look something like this: for(;;){ if(atomic_swap(&thefutex, 1) == 0) break; // we own the lock now // slow path, go to sleep until awoken syscall(SYS_FUTEX, FUTEX_WAIT, ...); }