LWN.net Logo

What is a spinlock?

What is a spinlock?

Posted Jan 30, 2003 22:51 UTC (Thu) by perlid (guest, #6533)
Parent article: Fast reader/writer locks

Btw, can anyone explain to me what a spinlock is?
I've done some multithreaded programming, so I know a bit about locks, but I don't know how a spinlock is constructed, and what the differences are between a spinlock and other locks.


(Log in to post comments)

What is a spinlock?

Posted Jan 30, 2003 22:54 UTC (Thu) by corbet (editor, #1) [Link]

A spinlock is a lightweight lock with no wait queues; if there is contention for the lock one (or more) threads will "spin" (busy-wait) until the lock is free. They are usually built with some sort of atomic test-and-set instruction; a quick look in the kernel source will show how they are done for any particular architecture.

What is a spinlock?

Posted Jan 31, 2003 18:19 UTC (Fri) by cpeterso (guest, #305) [Link]

With a normal lock, a thread will be put to sleep when it has to wait to obtain the lock. When the lock is available, the thread will be woken up.

As described, a thread will "spin" or "busy-wait" for a spinlock. Thus spinlocks should only be used for very short critical sections. If the spinning thread has to wait a long time, your CPU usage will hit 100% because the spinning thread is executing a very tight loop. That is obviously bad for performance.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds