3 ms·
>"That's a common misconception, obtaining an uncontended mutex (futex) is just a single atomic operation that takes about 25 nanoseconds" A futex is just a sp
by bogomipz 9y ago
>"That's a common misconception, obtaining an uncontended mutex (futex) is just a single atomic operation that takes about 25 nanoseconds"
A futex is just a specific implementation of a mutex though right? Or am I reading your comment wrong? I am curious where the 25 ns comes from.
Also isn't what makes a futex so fast is that it's done in user space - hence the name?
- exDM69 9y agoThe 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, ...); }