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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:03:31+00:00 2026-05-13T13:03:31+00:00

I know that the .NET memory model (on the .NET Framework; not compact/micro/silverlight/mono/xna/what-have-you) guaranteed

  • 0

I know that the .NET memory model (on the .NET Framework; not compact/micro/silverlight/mono/xna/what-have-you) guaranteed that for certain types (most notably primitive integers and references) operations were guaranteed to be atomic.

Further, I believe that the x86/x64 test-and-set instruction (and Interlocked.CompareExchange) actually references the global memory location, so if it succeeds another Interlocked.CompareExchange would see the new value.

Finally, I believe that the volatile keyword is an instruction to the compiler to propagate reads & writes ASAP and to not reorder operations concerning this variable (right?).

This leads to a few questions:

  1. Are my beliefs above correct?
  2. Interlocked.Read does not have an overload for int, only for longs (which are 2 WORDs and thus are not normally read atomically). I always assumed that the .NET memory model guaranteed that the newest value would be seen when reading ints/references, however with processor caches, registers, etc. I’m starting to see this may not be possible. So is there a way to force the variable to be re-fetched?
  3. Is volatile sufficient to solve the above problem for integers and references?
  4. On x86/x64 can I assume that…

If there are two global integer variables x and y, both initialized to 0 that if I write:

x = 1;
y = 2;

That NO thread will see x = 0 and y = 2 (i.e. the writes will occur in order). Does this change if they are volatile?

  • 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-13T13:03:31+00:00Added an answer on May 13, 2026 at 1:03 pm
    • Only reads and writes to variables that are at most 32 bits wide (and 64 bits wide on x64 systems) are atomic. All this means is that you won’t read an int and get a half-written value. It doesn’t mean arithmetic is atomic.
    • The interlocked operations also act as memory barriers, so yes, Interlocked.CompareExchange will see the updated value.
    • See this page. Volatile does not mean ordered. Some compilers may choose not to re-order operations on volatile variables, but the CPU is free to re-order. If you want to stop the CPU from re-ordering instructions, use a (full) memory barrier.
    • The memory model ensures that reads and writes are atomic, and using the volatile keyword ensures that reads will always come from memory, not from a register. So you will see the newest value. This is because x86 CPUs will invalidate the cache when appropriate – see this and this. Also, see InterlockedCompareExchange64 for how to atomically read 64-bit values.
    • And finally, the last question. The answer is a thread could in fact see x = 0 and y = 2, and using the volatile keyword doesn’t change that because the CPU is free to re-order instructions. You need a memory barrier.

    Summary:

    1. The compiler is free to re-order instructions.
    2. The CPU is free to re-order instructions.
    3. Word-sized reads and writes are atomic. Arithmetic and other operations are not atomic because they involve a read, compute, then write.
    4. Word-sized reads from memory will always retrieve the newest value. But most of the time you don’t know if you’re actually reading from memory.
    5. A full memory barrier stops (1) and (2). Most compilers allow you to stop (1) by itself.
    6. The volatile keyword ensures you’re reading from memory – (4).
    7. The interlocked operations (the lock prefix) allow multiple operations to be atomic. For example, a read + write (InterlockedExchange). Or a read + compare + write (InterlockedCompareExchange). They also act as memory barriers, so (1) and (2) are stopped. They always write to memory (obviously), so (4) is ensured.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We know that behind the scenes, the ASP.NET MVC framework will use reflection to
Occasionally I will know that there is a .NET Framework function that returns a
I know that .Net framework runtime is contained in mscoree.dll.Can anyone help me in
I have an application in .net that gets more and more memory. I've got
I know that .NET assembly is self-descriptive because of the metadata. Now suppose I
i know that .NET String class uses such behaviour, but I can't find any
I know that ASP.NET MVC 1.0 is bin-deployable as explained in Phil Haack's article.
i know that in vb.net you can just do Exit Sub but i would
I know that the static objects in .Net managed world are loaded in Loader
I know that the reason that Microsoft came out with ASP.NET MVC was to

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.