Questions tagged [spinlock]

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.

318 questions
375
votes
8 answers

When should one use a spinlock instead of mutex?

I think both are doing the same job,how do you decide which one to use for synchronization?
compile-fan
  • 16,885
  • 22
  • 59
  • 73
137
votes
11 answers

What exactly are "spin-locks"?

I always wondered what they are: every time I hear about them, images of futuristic flywheel-like devices go dancing (rolling?) through my mind... What are they?
RCIX
  • 38,647
  • 50
  • 150
  • 207
52
votes
4 answers

How does x86 pause instruction work in spinlock *and* can it be used in other scenarios?

The pause instruction is commonly used in the loop of testing spinlock, when some other thread owns the spinlock, to mitigate the tight loop. It's said that it is equivalent to some NOP instructions. Could somebody tell me how exactly it works for…
Infinite
  • 3,198
  • 4
  • 27
  • 36
43
votes
10 answers

Spinlocks, How Useful Are They?

How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks? Personally, when I write some sort of code that requires thread…
unknown
40
votes
4 answers

onSpinWait​() method of Thread class - Java 9

While learning Java 9 features I came across a new method of Thread class, called onSpinWait​. As per javadocs, this method is used for this: Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
40
votes
11 answers

Is my spin lock implementation correct and optimal?

I'm using a spin lock to protect a very small critical section. Contention happens very rarely so a spin lock is more appropriate than a regular mutex. My current code is as follows, and assumes x86 and GCC: volatile int exclusion = 0; void lock()…
Hongli
  • 18,682
  • 15
  • 79
  • 107
38
votes
6 answers

spin_lock_irqsave vs spin_lock_irq

On an SMP machine we must use spin_lock_irqsave and not spin_lock_irq from interrupt context. Why would we want to save the flags (which contain the IF)? Is there another interrupt routine that could interrupt us?
cojocar
  • 1,782
  • 2
  • 18
  • 22
31
votes
3 answers

Difference between Mutex, Semaphore & Spin Locks

I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and…
Novice
  • 540
  • 2
  • 8
  • 21
26
votes
5 answers

Fastest inline-assembly spinlock

I'm writing a multithreaded application in c++, where performance is critical. I need to use a lot of locking while copying small structures between threads, for this I have chosen to use spinlocks. I have done some research and speed testing on…
sigvardsen
  • 1,531
  • 3
  • 26
  • 44
22
votes
3 answers

Is memory barrier or atomic operation required in a busy-wait loop?

Consider the following spin_lock() implementation, originally from this answer: void spin_lock(volatile bool* lock) { for (;;) { // inserts an acquire memory barrier and a compiler barrier if (!__atomic_test_and_set(lock,…
gavv
  • 4,649
  • 1
  • 23
  • 40
20
votes
3 answers

Spinlock vs Busy wait

Please explain why Busy Waiting is generally frowned upon whereas Spinning is often seen as okay. As far as I can tell, they both loop infinitely until some condition is met.
PMcK
  • 393
  • 1
  • 2
  • 11
19
votes
1 answer

SpinLock throwing SynchronizationLockException

I am trying to use SpinLock, but even this most basic code in a single threaded Console app throws the following exception when I callSpinLock.Exit() System.Threading.SynchronizationLockException was unhandled by user code Message=The calling…
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
17
votes
4 answers

Why disabling interrupts disables kernel preemption and how spin lock disables preemption

I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption. In the "Interrupt Control" section of chapter 7, it says: Moreover, disabling interrupts also disables kernel preemption. I also read from…
feirainy
  • 497
  • 1
  • 4
  • 11
17
votes
1 answer

High system CPU usage when contending futex

I have observed that when the linux futexes are contended, the system spends A LOT of time in the spinlocks. I noticed this to be a problem even when futexes are not used directly, but also when calling malloc/free, rand, glib mutex calls, and…
Alex Fiddler
  • 213
  • 1
  • 3
  • 8
16
votes
3 answers

x86 spinlock using cmpxchg

I'm new to using gcc inline assembly, and was wondering if, on an x86 multi-core machine, a spinlock (without race conditions) could be implemented as (using AT&T syntax): spin_lock: mov 0 eax lock cmpxchg 1 [lock_addr] jnz…
ManRow
  • 1,563
  • 4
  • 21
  • 40
1
2 3
21 22