Earlier quoted context omitted.
Comment-OP here - this is also more or less my use case. Whether there is a use case for spinlocks outside low-latency trading, i have no idea!
a friend of mine recently told me how Windows schedules cpu-bound threads to different cores to prevent thermal throttling, so I now wonder if we were mucking things up by running our CPUs too hot
Mutexes are faster than Spinlocks
71–80 of 150 posts
Re: Mutexes are faster than Spinlocks
#72Also, there are better spinlock implementations, such as speculative spinlocks, and queued locks.
Re: Mutexes are faster than Spinlocks
#73Earlier quoted context omitted.
It seems to be doing similar logic. 1. Does a CAS with compare_exchange_weak() at line 69. 2. Then call lock_slow() at line 72 to do spinlocking (Guh!). 3. The call to parking_lot_core::park() at line 256 seems to sleep wait.
So this it acquires fully in userspace if there's no contention, it even spins if there is contention, and then if that wasn't enough it lets the thread sleep with the timeout. Which matches the description of that library: https://github.com/Amanieu/parking_lot "This library provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library"…
I would imagine the unlocking call would wake up the sleeping thread so it won't keep waking up periodically.
The user-mode only implementation sounds interesting. The only downside is it won't work with inter-process locking.
Re: Mutexes are faster than Spinlocks
#74Earlier quoted context omitted.
Also if both threads are pinned to separate cores and nothing else is supposed to run on those cores, it is pointless to use anything but spinlocks as there is no other thread that could better use the core (and probably you do not want the core to go to a low power syate waiting for an interrupt).
You're discounting energy use. This is a bad strategy on a battery powered device.
Re: Mutexes are faster than Spinlocks
#75Earlier quoted context omitted.
TFA? Tried to google but unsuccessful
I've seen lots of valid variants, including and probably not limited to: The feature article ; the freaking article ; the friendly article ; the f$%^&*( article, c.f. RTFM.
And yeah, I believe that it derives from "RTFA" (read the fucking article) which would be what you told people who obviously commented without reading the article.
Re: Mutexes are faster than Spinlocks
#76Earlier quoted context omitted.
You're misreading the benchmark, that's 6ms for 10,000 lock/unlocks per thread, 320,000 lock/unlocks total. In other words 0.6 microseconds per thread per lock.
That's still unreasonably high, isn't it? Even a Go sync.Mutex, not exactly a hot-rod implementation, can be acquired and released in < 50ns on the garbage hardware I have before me.
Re: Mutexes are faster than Spinlocks
#77The author has an implicit definition of "faster" which it is important to be aware of. The main use of spinlocks that i'm aware of is minimising latency in inter-processor communication. That is, if you have a worker task which is waiting for a supervisor task to tell it to do something, then to minimise the time between the supervisor giving the order and the worker getting to work, use a spinlock. For this to real…
>The main use of spinlocks that i'm aware of is minimising latency in inter-processor communication. The main use of spinlocks that I'm aware of is dealing with interrupt handlers in driver code. In this situation you generally can't go to sleep (sleeping with interrupts disabled is generally a good way of never waking up) so calling "mutex_lock" is simply out of the question. That's probably a niche use but that's l…
Re: Mutexes are faster than Spinlocks
#78Earlier quoted context omitted.
You're discounting energy use. This is a bad strategy on a battery powered device.
Intel has a low-power PAUSE instruction that is literally a ‘rep nop’. I assume Arm has one too.
Re: Mutexes are faster than Spinlocks
#79Earlier quoted context omitted.
So this it acquires fully in userspace if there's no contention, it even spins if there is contention, and then if that wasn't enough it lets the thread sleep with the timeout. Which matches the description of that library: https://github.com/Amanieu/parking_lot "This library provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library"…
Ah, thanks for the info. I would imagine the unlocking call would wake up the sleeping thread so it won't keep waking up periodically. The user-mode only implementation sounds interesting. The only downside is it won't work with inter-process locking.
Re: Mutexes are faster than Spinlocks
#80I'm really curious how macOS's os_unfair_lock compares here.
Is that implementation open source? Don’t remember which dyld contains it.