# Reinventing spinlocks

DevFeed: [Reinventing spinlocks](<https://devfeed.tech/articles/reinventing-spinlocks-38911.md>)

Original publisher: [Read original article](<https://idea.popcount.org/2012-09-12-reinventing-spinlocks>)

Author: Marek

Published: 2012-09-11T22:00:00Z

Content type: tutorial

Language: en

Sources: [Marek Majkowski](<https://devfeed.tech/sources/marek-majkowski.md>)

Topics: [C](<https://devfeed.tech/topics/c.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [Code](<https://devfeed.tech/topics/code.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [gcc](<https://devfeed.tech/topics/gcc.md>), [x86](<https://devfeed.tech/topics/x86.md>), [mac os](<https://devfeed.tech/topics/mac-os.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [c](<https://devfeed.tech/tags/c.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [library](<https://devfeed.tech/tags/library.md>), [mac-os](<https://devfeed.tech/tags/mac-os.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

This tutorial compares two spinlock implementations in C using compare-and-swap and a conditional store. It explains the required compiler barrier, notes architecture-specific ordering considerations, and reports benchmark results for contended and uncontended cases.

## Source excerpt

Reinventing spinlocks In the previous article I was playing with the implementation of the concurrent Queue in C. During the experiments I tried to outsmart library and beat the speed of their spinlock implementation. CAS/CAS My first attempt was to use compare-and-swap instruction for the spinlock. Here's the code, basically we spin till the lock is acquired: This approach works well, but we can still do better. CAS/store It's easy to notice that the doesn't actually need the heavy write.