# A Close Look at a Spinlock

DevFeed: [A Close Look at a Spinlock](<https://devfeed.tech/articles/a-close-look-at-a-spinlock-39748.md>)

Original publisher: [Read original article](<https://blog.regehr.org/archives/2173>)

Author: regehr

Published: 2021-11-06T19:57:06Z

Content type: article

Language: en

Sources: [Embedded in Academia](<https://devfeed.tech/sources/embedded-in-academia.md>)

Topics: [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [Operating system](<https://devfeed.tech/topics/operating-system.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Code](<https://devfeed.tech/topics/code.md>), [Hardware](<https://devfeed.tech/topics/hardware.md>), [x86](<https://devfeed.tech/topics/x86.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [code](<https://devfeed.tech/tags/code.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [hardware](<https://devfeed.tech/tags/hardware.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [processor](<https://devfeed.tech/tags/processor.md>), [synchronization](<https://devfeed.tech/tags/synchronization.md>), [threads](<https://devfeed.tech/tags/threads.md>), [uncategorized](<https://devfeed.tech/tags/uncategorized.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

This article explains how spinlocks provide mutual exclusion on multiprocessor systems. It covers why naive load-and-store implementations can fail on modern weak-memory architectures, how GCC atomic intrinsics can implement a portable user-mode spinlock, and how FIFO spinlocks can improve fairness under contention.

## Source excerpt

The spinlock is the most basic mutual exclusion primitive provided by a multiprocessor operating system. Spinlocks need to protect against preemption on the current CPU (typically by disabling interrupts, but we'll ignore that aspect in this post) and also against attempts by other cores to concurrently access the critical section (by using atomic memory operations). [...]