A spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available.
Questions tagged [spinlock]
318 questions
16
votes
4 answers
Is spin lock useful in a single processor uni core architecture?
I am confused by the function of spin lock.
The spin lock is used to stop the process from re-scheduling.
However, in a machine with just one core, is it useful to use spin lock
to prevent context switching?
user53670
13
votes
2 answers
Linux Kernel: Spinlock SMP: Why there is a preempt_disable() in spin_lock_irq SMP version?
The original code in Linux kernel is:
static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
{
local_irq_disable();
preempt_disable();
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
LOCK_CONTENDED(lock, do_raw_spin_trylock,…

Zhi Wang
- 131
- 1
- 3
11
votes
1 answer
Understanding link between CONFIG_SMP, Spinlocks and CONFIG_PREEMPT in latest (3.0.0 and above) Linux kernel
To give you full context my discussion begun with an observation that I am running a SMP linux (3.0.1-rt11) on ARM cortex A8 based SoC which is a uniprocessor. I was curious to know if there will be any performance advantage by disabling SMP…

Satpal Parmar
- 361
- 3
- 12
10
votes
3 answers
Do spin locks always require a memory barrier? Is spinning on a memory barrier expensive?
I wrote some lock-free code that works fine with local
reads, under most conditions.
Does local spinning on a memory read necessarily imply I
have to ALWAYS insert a memory barrier before the spinning
read?
(To validate this, I managed to produce a…

blais
- 687
- 7
- 9
10
votes
3 answers
Are there any examples in the .Net framework that use spinlock or spinwait?
I had a look at the concurrent collections but they appear to use normal locking underneath the hood. Are there any good examples in the .Net framework that use this locking construct?
I initially looked at ConcurrentDictionary. I saw it was using…

uriDium
- 13,110
- 20
- 78
- 138
10
votes
1 answer
Why everyone states that SpinLock is faster?
I have read a lot of docs and articles and posts all over the internet.
Almost everyone and everywhere commits that SpinLock is faster for a short running pieces of code, but I made a test, and it appears to me that simple Monitor.Enter works faster…

Rauf
- 312
- 3
- 16
9
votes
1 answer
Is linux release the spinlock/semaphore when it kill a process?
If a process holds some spinlocks or semaphores, and exit accidently(e.g., killed by linux), would linux release these locks correctly?
If linux doesn't do this work, why?

silverbullettt
- 846
- 1
- 10
- 13
9
votes
5 answers
Why spinlocks don't work in uniprocessor (unicore) systems?
I know that spinlocks work with spining, different kernel paths exist and Kernels are preemptive, so why spinlocks don't work in uniprocessor systems? (for example, in Linux)

user683595
- 397
- 1
- 3
- 10
8
votes
3 answers
Equivalent of x86 PAUSE instruction for PPC
Does there exist an equivalent of the x86 PAUSE instruction, which is placed within busy waiting loops to improve performance, particularly on SMT machines, on PowerPC?

Jimmeh
- 1,011
- 9
- 14
8
votes
1 answer
Is a spinlock lock free?
I am a little bit confused about the two concepts.
definition of lock-free on wiki:
A non-blocking algorithm is lock-free if there is guaranteed
system-wide progress
definition of non-blocking:
an algorithm is called non-blocking if failure or…

du369
- 821
- 8
- 22
8
votes
2 answers
Spinlock with XCHG unlocking
The example implementation Wikipedia provides for a spinlock with the x86 XCHG command is:
; Intel syntax
locked: ; The lock variable. 1 = locked, 0 = unlocked.
dd 0
spin_lock:
mov eax, 1 ; Set the…

mowwwalker
- 16,634
- 25
- 104
- 157
8
votes
2 answers
Spinlock vs std::mutex::try_lock
What are the benefits of using a specifically designed spinlock (e.g. http://anki3d.org/spinlock) vs. code like this:
std::mutex m;
while (!m.try_lock()) {}
# do work
m.unlock();

user2411693
- 533
- 4
- 14
8
votes
2 answers
Do spin_lock and spin_unlock hurt performance of an SMP kernel on a single-cpu machine?
On my Ubuntu machine, default kernel image which is running is built for smp (CONFIG_SMP=y). But this machine has only 1 cpu.
On uni-processor kernel, unlike smp kernel, spin_lock/unlock are null functions.
So how does spin_lock() and spin_unlock()…

Vinit Dhatrak
- 6,814
- 8
- 27
- 30
8
votes
3 answers
Intel Inspector reports a data race in my spinlock implementation
I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable);
The program seems to work OK (it gives the same result every time, which is not the case when no…

Gratian Lup
- 1,485
- 3
- 19
- 29
7
votes
4 answers
Does Mac OS X have pthread_spinlock_t type?
I didn't find it in Mac, but almost all Linux os support it..
Any one knows how to port it to mac?

JustQieTry
- 491
- 1
- 6
- 13