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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:47:53+00:00 2026-06-11T22:47:53+00:00

What is the difference between Interlocked.Exchange and Volatile.Write ? Both methods update value of

  • 0

What is the difference between Interlocked.Exchange and Volatile.Write?

Both methods update value of some variable. Can someone summarize when to use each of them?

  • Interlocked.Exchange
  • Volatile.Write

In particular I need to update double item of my array, and I want another thread to see the freshest value. What is preferred? Interlocked.Exchange(ref arr[3], myValue) or Volatile.Write(ref arr[3], info); where arr is declared as double?


Real example, I declare double array like that:

private double[] _cachedProduct;

In one thread I update it like that:

_cachedProduct[instrumentId] = calcValue;
//...
are.Set();

In another thread I read this array like that:

while(true)
{
    are.WaitOne();
    //...
    result += _cachedProduct[instrumentId];
    //...
}

For me it just works fine as is. However to make sure "it will always work" no matter what it seems I should add either Volatile.Write or Interlocked.Exchange. Because double update is not guaranteed to be atomic.

In the answer to this question I want to see detailed comparison of Volatile and Interlocked classes. Why we need 2 classes? Which one and when to use?


Another example, from the implementation of a locking mechanism in an in-production project:

private int _guard = 0;

public bool Acquire() => Interlocked.CompareExchange(ref _guard, 1, 0) == 0;

public void Release1() => Interlocked.Exchange(ref _guard, 0);
public void Release2() => Volatile.Write(ref _guard, 0);

Does it make any practical difference if the users of this API call the Release1 or the Release2 method?

  • 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-11T22:47:54+00:00Added an answer on June 11, 2026 at 10:47 pm

    the Interlocked.Exchange uses a processor instruction that guarantees an atomic operation.

    The Volatile.Write does the same but it also includes a memory barrier operation.
    I think Microsoft added Volatile.Write on DotNet 4.5 due to support of ARM processors on Windows 8. Intel and ARM processors differs on memory operation reordering.

    On Intel, you have a guarantee that memory access operations will be done in the same order they are issued, or at least that a write operation won’t be reordered.

    From Intel® 64 and IA-32 Architectures Software Developer’s Manual, Chapter 8:

    8.2.2 Memory Ordering in P6 and More Recent Processor Families The Intel Core 2 Duo, Intel Atom, Intel Core Duo, Pentium 4, and P6 family
    processors also use a processor-ordered memory-ordering model that can
    be further defined as “write ordered with store-buffer forwarding.”
    This model can be characterized as follows.

    On ARM you don’t have this kind of guarantee, so a memory barrier is required. An ARM blog explaining this can be found here: http://blogs.arm.com/software-enablement/594-memory-access-ordering-part-3-memory-access-ordering-in-the-arm-architecture/

    In your example, as the operation with double is not guaranteed to be atomic, I would recommend a lock to access it. Remember that you have to use the lock on both parts of your code, when reading and setting the value.

    A more complete example would be better to answer your question, as it is not clear what happens after these values are set. For a vector, if you have more readers than writers, consider the use of a ReaderWriterLockSlim object: http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx

    The number of threads and the frequency of read/writes can change dramatically your locking strategy.

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

Sidebar

Related Questions

Can any one explain difference between position and anchor point in cocos-2D with some
Difference between Map and Properties as both have key-value pair.
What is the logical difference between resource and resources methods Here is some examples:
Can someone explain the difference between a servlet filter and tomcat valve. What is
What difference between this methods, why SortDropDown is correct but Page_load and GetCases are
Is there a difference between the two expressions: (*x)++ and ++(*x) I can see
What is the difference between the two classes below? Do you have some related
Is there a difference between using one method over the other. You can either
Difference between value parameter and reference parameter ? This question is asked sometime by
Is there a performance difference between i++ and ++i if the resulting value is

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.