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 839781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:31:09+00:00 2026-05-15T05:31:09+00:00

Why is CompareAndSwap instruction considered expensive? I read in a book: Memory barriers are

  • 0

Why is CompareAndSwap instruction considered expensive?

I read in a book:

“Memory barriers are expensive, about as
expensive as an atomic compareAndSet()
instruction.”

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-05-15T05:31:10+00:00Added an answer on May 15, 2026 at 5:31 am

    “CAS isn’t appreciably different than a normal store. Some of the misinformation regarding CAS probably arises from the original implementation of lock:cmpxchg (CAS) on Intel processors. The lock: prefix caused the LOCK# signal to be asserted, acquiring exclusive access to the bus. This didn’t scale of course. Subsequent implementations of lock:cmpxchg leverage cache coherency protocol — typically snoop-based MESI — and don’t assert LOCK#.” – David Dice, Biased locking in HotSpot

    “Memory barriers are expensive, about as expensive as an atomic compareAndSet() instruction.”

    This is quite true.
    E.g. on x86, a proper CAS on a multi-processor system has a lock prefix.
    The lock prefix results in a full memory barrier:

    “…locked operations serialize all outstanding load and store operations (that is, wait for them to complete).” …”Locked operations are atomic with respect to all other memory operations and all externally visible events. Only instruction fetch and page table accesses can pass locked instructions. Locked instructions can be used to synchronize data written by one processor and read by another processor.” – Intel® 64 and IA-32 Architectures Software Developer’s Manual, Chapter 8.1.2.

    A memory barrier is in fact implemented as a dummy LOCK OR or LOCK AND in both the .NET and the JAVA JIT on x86/x64.
    On x86, CAS results in a full memory barrier.

    On PPC, it is different. An LL/SC pair – lwarx & stwcx – can be used to load the memory operand into a register, then either write it back if there was no other store to the target location, or retry the whole loop if there was. An LL/SC can be interrupted.
    It also does not mean an automatic full fence.
    Performance characteristics and behaviour can be very different on different architectures.
    But then again – a weak LL/SC is not CAS.

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

Sidebar

Related Questions

Assuming that CompareAndSwap (or CAS) never fails spuriously, can CompareExchange be implemented with CAS?
I've been reading about non-blocking approaches for some time. Here is a piece of
I needed a simple non-blocking static-block-size memory-pool. I didn't find such on the web.
I read one article, describing the ABA problem, but there is something, that I
While testing a program for scalability, I came across the situation where I have
I have a struct called Ambigous, and inside the struct I have an array

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.