Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is pthread_spin_lock the right thing to use (over e.g. a pthread mutex)?

Tags:

c

pthreads

Given that pthread_spin_lock is available, when would I use it, and when should one not use them ?

i.e. how would I decide to protect some shared data structure with either a pthread mutex or a pthread spinlock ?

like image 864
Lyke Avatar asked Jul 06 '11 21:07

Lyke


People also ask

What is Pthread_spin_lock?

The pthread_spin_unlock() function unlocks the spin lock referred to lock. If any threads are spinning on the lock, one of those threads will then acquire the lock. Calling pthread_spin_unlock() on a lock that is not held by the caller results in undefined behavior.

What are spin locks are they better then mutex?

Spinlock is a lock which causes a thread trying to acquire it to simply wait in the loop and repeatedly check for its availability. In contrast, a mutex is a program object that is created so that multiple processes can take turns sharing the same resource. Thus, this is the main difference between spinlock and mutex.

Is futex faster than mutex?

So it's a statistical fact that in most operating systems that are POSIX compliant the pthread mutex is implemented in kernel space and is slower than a futex.

Does mutex use spinlock?

A hybrid mutex behaves like a spinlock at first on a multi-core system. If a thread cannot lock the mutex, it won't be put to sleep immediately, since the mutex might get unlocked pretty soon, so instead the mutex will first behave exactly like a spinlock.


1 Answers

The short answer is that a spinlock can be better when you plan to hold the lock for an extremely short interval (for example to do nothing but increment a counter), and contention is expected to be rare, but the operation is occurring often enough to be a potential performance bottleneck. The advantages of a spinlock over a mutex are:

  1. On unlock, there is no need to check if other threads may be waiting for the lock and waking them up. Unlocking is simply a single atomic write instruction.
  2. Failure to immediately obtain the lock does not put your thread to sleep, so it may be able to obtain the lock with much lower latency as soon a it does become available.
  3. There is no risk of cache pollution from entering kernelspace to sleep or wake other threads.

Point 1 will always stand, but point 2 and 3 are of somewhat diminished usefulness if you consider that good mutex implementations will probably spin a decent number of times before asking the kernel for help waiting.

Now, the long answer:

What you need to ask yourself before using spinlocks is whether these potential advantages outweigh one rare but very real disadvantage: what happens when the thread that holds the lock gets interrupted by the scheduler before it can release the lock. This is of course rare, but it can happen even if the lock is just held for a single variable-increment operation or something else equally trivial. In this case, any other threads attempting to obtain the lock will keep spinning until the thread the holds the lock gets scheduled and has a chance to release the lock. This may never happen if the threads trying to obtain the lock have higher priorities than the thread that holds the lock. That may be an extreme case, but even without different priorities in play, there can be very long delays before the lock owner gets scheduled again, and worst of all, once this situation begins, it can quickly escalate as many threads, all hoping to get the lock, begin spinning on it, tying up more processor time, and further delaying the scheduling of the thread that could release the lock.

As such, I would be careful with spinlocks... :-)

like image 151
R.. GitHub STOP HELPING ICE Avatar answered Oct 23 '22 23:10

R.. GitHub STOP HELPING ICE