Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:15:03+00:00 2026-06-03T01:15:03+00:00

I was recently reading about the Compare And Swap atomic action (CMPXCHG, .NET’s Interlocked.CompareExchange,

  • 0

I was recently reading about the Compare And Swap atomic action (CMPXCHG, .NET’s Interlocked.CompareExchange, whatever).

I understand how it works internally, and how it’s used from a client.

What I can’t quite figure out is when would someone use CAS?

Wikipedia says:

CAS is used for implementing synchronization primitives like
semaphores and mutexes, likewise more sophisticated lock-free and
wait-free algorithms.

So, can anyone give me a more generic real-world use case with code and description of CAS usage?

This question is meant to be language-agnostic, so any language will do (C-based or x86 assembly preferred).

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:15:04+00:00Added an answer on June 3, 2026 at 1:15 am

    This is easy to see by example. Say we want to atomically and concurrently set a bit on a shared variable:

    int shared = 0;
    
    void Set(int index) {
     while (true) {
      if (Interlocked.CompareExchange<int>(ref shared, shared | (1 << index), shared) == shared)
       break; //success
     }
    }
    

    We detect failure if we see that the “old value” (which is the return value) has changed in the meantime.

    If this did not happen we did not have a concurrent modification so our own modification went through successfully.

    You can realize pretty complex stuff using this technique. The more complex the more performance loss through spinning, though.

    I want to emphasize that a key property of CAS is that it can fail and that failure can be detected reliably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started reading about ASP.net MVC and after getting excited about the concept,
Recently I was reading about partitioning code with .NET assemblies and stumbled upon a
Recently I have been reading up articles about DLL injection and I understand them
I was recently reading about artificial life and came across the statement, Conway’s Game
I was recently reading about the Arduino's Hello World application, and I thought to
I recently was reading about Oracle Index Organized Tables (IOTs) but am not sure
I have recently started reading about dependency injection and it has made me rethink
Recently I was reading about Codd's 12 Rules , and I understood all except
I've been reading recently about DI and IoC in C++. I am a little
I was recently reading an article in the bitsquid blog about how to manage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.