October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

CAS vs. a Lock: How One Benchmark Can Lead to the Wrong Conclusion

CAS and locks do different work under different conditions. Published examples show why a counter benchmark cannot establish a universal winner.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Neither compare-and-swap (CAS) nor a lock is inherently faster: the answer depends on the exact operation, contention, hardware, and implementation being compared. No reproducible record of the title’s promised first-person benchmark is available, so this article does not claim personal test results or invent a mid-test reversal. Published examples instead show why a result from one workload can mislead when treated as a universal rule.

What CAS and a lock actually do

Compare-and-swap is an atomic read-modify-write operation: it checks whether a memory location has an expected value and, if so, replaces it. In the Linux kernel, CAS is one operation in the atomic API, alongside operations such as atomic add and exchange. The API’s operations and their semantics involve architecture-specific considerations; the name of an operation alone does not tell you its cost on every processor. Linux kernel documentation: Atomic types, v6.6

As an Amazon Associate I earn from qualifying purchases.

A CAS loop adds a second layer: if the expected value no longer matches, the operation may be retried. Under contention, multiple threads can repeatedly compete to update the same location. A lock takes a different route: it coordinates access so that a thread holding the lock can perform protected work while others wait or contend for access.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That makes “CAS versus a lock” an incomplete comparison. A single CAS instruction, a retrying CAS loop, an atomic fetch-add, and a mutex-protected increment are distinct operations or algorithms. They may do different work and have different behavior under contention. Compare them only when they implement equivalent work.

Why one result can reverse your conclusion

The cost of a CAS loop is not just the cost of one successful attempt. Failed attempts consume work, and contention affects which thread can obtain ownership of the cache line containing the shared value. A source-level atomic increment may also compile into different machine instructions on different platforms. The compiler output, runtime, lock implementation, and memory-ordering choices therefore matter alongside the source code. Travis Downs, “A Concurrency Cost Hierarchy”

Downs’s maximum-contention, single-counter comparison is a useful example of how the answer can change with the implementation being tested: atomic add was significantly faster than his CAS loop, while std::mutex was competitive on the tested Skylake setup. That is a result for that benchmark and its machines—not a general ranking of atomics and locks. Travis Downs, “A Concurrency Cost Hierarchy,” July 6, 2020

A different workload could change the outcome. A single shared counter forces threads to compete over the same data, making it a deliberately narrow, high-contention test. An application that protects longer or different work, uses several independent data locations, or runs on another architecture is asking a different performance question.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What a published CAS benchmark example does—and does not—show

On September 30, 2026, Changbin Du proposed adding an atomic CAS benchmark to Linux perf bench. The proposal describes configurable thread and iteration counts and includes an example output. It is a mailing-list patch proposal, not evidence that the feature was accepted or released, and the figures below are example output—not an independently validated run or a comparison with a lock. Changbin Du, “[PATCH] perf bench: Add atomic CAS benchmark,” September 30, 2026

Proposal example detail Value
Threads 2
Iterations per thread 100,000,000
Repeats and warmup 10 repeats after 1 warmup run
Average wall-clock time 7,365.480 milliseconds; standard deviation 66.014 milliseconds
Total throughput 27,153,697 operations per second

The proposal’s benchmark synchronizes thread starts, uses a shared cache-line-aligned counter, excludes an initial warmup run, and reports aggregate and per-thread results. Those are useful measurement details, but a CAS-only example cannot answer whether CAS beats a lock: it does not provide an equivalent lock run under the same conditions.

How to make the comparison fair

Before timing, define the question precisely: which implementation completes the same protected task most efficiently for the workload you care about? A useful report makes the relevant conditions visible rather than presenting a bare time or throughput number.

  • Operation and algorithm: State whether the test uses one CAS attempt, a retrying CAS loop, an atomic fetch-add, or an increment protected by a named lock type. Ensure each version performs equivalent work.
  • Workload and contention: Describe the protected work, number of threads, and whether the test is single-threaded, lightly contended, or heavily contended. A shared counter is not a stand-in for every application’s critical section.
  • Platform and memory system: Name the processor and architecture, and explain relevant placement and sharing conditions, including cache behavior, alignment, operand size, and NUMA placement where applicable.
  • Implementation: Record the language, compiler and runtime, lock implementation, and memory-ordering choices. When possible, inspect the generated machine code instead of assuming that similar source expressions become the same instructions.
  • Measurement: Explain how threads begin, whether there is a warmup, how many trials are repeated, and whether results are wall-clock time, throughput, or both. Include variability and per-thread results where available; an aggregate can conceal uneven progress.

These details matter because atomic operations do not have one fixed performance relationship across processors. ETH Zurich’s SPCL project reports that “All the tested atomics have usually comparable latency and bandwidth,” referring to the operations and architectures evaluated in that study—not all processors or applications. ETH Zurich SPCL, “What is the true cost/performance of atomic operations?”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose for correctness first, then measure the real work

Performance is only one reason to choose an approach. CAS can serve as a basis for synchronization and lock-free algorithms, but building more elaborate operations on it can introduce complexity, scalability, and performance costs. Paul E. McKenney, Is Parallel Programming Hard, And, If So, What Can You Do About It?, version 2024.12.27a

Use a lock when it gives the clearest correct way to protect the shared state; use an atomic operation or CAS-based design when its semantics fit the problem. Then benchmark the real critical section on the target system. A counter microbenchmark can reveal costs under its own conditions, but it cannot establish a universal winner—or predict every application’s result.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.