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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:31:59+00:00 2026-05-18T04:31:59+00:00

I’m having some questions I’d like to clarify. Volatile read ensure you read the

  • 0

I’m having some questions I’d like to clarify.

  1. Volatile read ensure you read the
    latest value of a variable. Does it
    means that it forces to all CPU’s to
    flush their cached values for that
    variable? only that variable or all?
    So if it will force all CPUs to
    flush cached writes and get latest
    version from main memory, is this a
    memory barrier?

  2. Volatile write ensure you write a
    value to the variable in main
    memory. Does it means that it void
    all the cached values for that
    variable in all CPUs?

  3. Are you using a memory barrier when
    you use the keyword volatile?

  4. Interloked performs a
    read/modify/write in an atomic
    operation. Does Interlocked ensure
    that you are for example
    incrementing the latest version of a
    variable and the other CPUs will see
    this change? I think so because it’s
    supposed to use a memory barrier,
    but I’m not sure. So could we say
    that Interloked is doing a
    VolatileRead/modify/VolatileWrite
    atomically?

  5. When you use a memory barrier, does
    it affect to all variables in all
    CPUS, or just the surrounding ones?

  6. Locking is expensive because it
    causes two memory barriers and a
    “context switch” if the thread has
    to wait, but then what is the
    advantage of Interlocked? just to
    avoid the “context switch”?

  7. What is the deal with
    ReaderWriterLockSlim and
    recursivity? I didn’t understans
    what is the issue.

As you can see, I have a total mess in my mind right now.

Thanks in advance.

  • 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-18T04:32:00+00:00Added an answer on May 18, 2026 at 4:32 am

    Before answering your questions I should point out that memory barriers affect more than just the CPU. There are really two memory models in play when an application runs. One is at the hardware level and the other is at the software level. As a developer you have to code for the weakest combination of the different elements from both. With the CLR and an x86 architecture this usually means the CLR is more important because the x86 architecture actually has a fairly strong memory model. In other words, the volatile keyword and other memory barriers mechanisms will affect how the JIT produces code as well.

    1.Volatile read ensure you read the latest value of a variable. Does it
    means that it forces to all CPU’s to
    flush their cached values for that
    variable? only that variable or all?
    So if it will force all CPUs to flush
    cached writes and get latest version
    from main memory, is this a memory
    barrier?

    First, technically a volatile read does not ensure you read the latest value of a variable. All it actually means is that no other read or write can occur before the volatile one. However, the effect is that the read has to come from main memory if it is preceded by another volatile read. Second, no, a volatile read has no influence on other writes so it does not force all CPUs to flush their write cache. Third, yes a volatile operation is considered a memory barrier.

    2.Volatile write ensure you write a value to the variable in main memory.
    Does it means that it void all the
    cached values for that variable in all
    CPUs?

    Similiar to a volatile read, a volatile write is technically about ordering. It ensures that no other read or write can occur after the volatile one. It does not mean that the write in question immediately gets committed. It only affects the CPU executing that thread. Interestingly, the x86 architecture actually treats all writes as volatile. But, the CLR does not (at least the ECMA specification). That is why you still have to use a volatile operation on writes. This is one example of coding for the weakest memory model element from both the hardware and software level.

    3.Are you using a memory barrier when you use the keyword volatile?

    Yes. There are two types of memory barriers. Full fences and half fences. Half fences can either guarentee acquire semantics (volatile read) or release semantics (volatile write), but not both at the same time. A full fence (via Thread.MemoryBarrier for example) guarentees both.

    4.Interloked performs a read/modify/write in an atomic
    operation. Does Interlocked ensure
    that you are for example incrementing
    the latest version of a variable and
    the other CPUs will see this change? I
    think so because it’s supposed to use
    a memory barrier, but I’m not sure. So
    could we say that Interloked is doing
    a VolatileRead/modify/VolatileWrite
    atomically?

    Yep. And for what is worth the interlocked increment and decrement operations can be implemented with a CAS operation. In .NET you would use the Interlocked.CompareExchange method in a loop until the operation succeeded. I bet the Interlocked.Increment and Interlocked.Decrement methods use a native CPU instruction though, but I am prepared to be wrong about that.

    5.When you use a memory barrier, does it affect to all variables in all
    CPUS, or just the surrounding ones?

    It will affect all memory accesses, but only on the CPU executing that thread.

    6.Locking is expensive because it causes two memory barriers and a
    “context switch” if the thread has to
    wait, but then what is the advantage
    of Interlocked? just to avoid the
    “context switch”?

    Yeah basically. The thread never blocks with interlocked operations.

    7.What is the deal with ReaderWriterLockSlim and recursivity?
    I didn’t understans what is the issue.

    You cannot acquire the lock twice on the same thread without first releasing it. There is more information on Joe Duffy’s blog.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I have a French site that I want to parse, but am running into

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.