Reading an int that's updated by Interlocked on other threads

how to read integer in java using scanner
string to int java
how to take integer input in java using scanner
how to read integer in java using bufferedreader
scanner java
how to read string in java
how to take integer input from user in java without using scanner
array input in java

(This is a repeat of: How to correctly read an Interlocked.Increment'ed int field? but, after reading the answers and comments, I'm still not sure of the right answer.)

There's some code that I don't own and can't change to use locks that increments an int counter (numberOfUpdates) in several different threads. All calls use:

Interlocked.Increment(ref numberOfUpdates);

I want to read numberOfUpdates in my code. Now since this is an int, I know that it can't tear. But what's the best way to ensure that I get the latest value possible? It seems like my options are:

int localNumberOfUpdates = Interlocked.CompareExchange(ref numberOfUpdates, 0, 0);


int localNumberOfUpdates = Thread.VolatileRead(numberOfUpdates);

Will both work (in the sense of delivering the latest value possible regardless of optimizations, re-orderings, caching, etc.)? Is one preferred over the other? Is there a third option that's better?

I'm a firm believer in that if you're using interlocked to increment shared data, then you should use interlocked everywhere you access that shared data. Likewise, if you use insert you favorite synchronization primitive here to increment shared data, then you should use insert you favorite synchronization primitive here everywhere you access that shared data.

int localNumberOfUpdates = Interlocked.CompareExchange(ref numberOfUpdates, 0, 0);

Will give you exactly what your looking for. As others have said interlocked operations are atomic. So Interlocked.CompareExchange will always return the most recent value. I use this all the time for accessing simple shared data like counters.

I'm not as familiar with Thread.VolatileRead, but I suspect it will also return the most recent value. I'd stick with interlocked methods, if only for the sake of being consistent.

Additional info:

I'd recommend taking a look at Jon Skeet's answer for why you may want to shy away from Thread.VolatileRead(): Thread.VolatileRead Implementation

Eric Lippert discusses volatility and the guarantees made by the C# memory model in his blog at Straight from the horses mouth: "I don't attempt to write any low-lock code except for the most trivial usages of Interlocked operations. I leave the usage of "volatile" to real experts."

And I agree with Hans's point that the value will always be stale at least by a few ns, but if you have a use case where that is unacceptable, its probably not well suited for a garbage collected language like C# or a non-real-time OS. Joe Duffy has a good article on the timeliness of interlocked methods here:

How to read integer value from the standard input in Java, You can use java.util.Scanner (API): import java.util.Scanner; // Scanner in = new Scanner(; int num = in.nextInt();. It can also tokenize input with  To read inputs as integers in C#, use the Convert.ToInt32() method. res = Convert.ToInt32(val); Let us see how − The Convert.ToInt32 converts the specified string representation of a number to an equivalent 32-bit signed integer. Firstly, read the console input − string val; val = Console.ReadLine(); After reading, convert it to an integer.

Thread.VolatileRead(numberOfUpdates) is what you want. numberOfUpdates is an Int32, so you already have atomicity by default, and Thread.VolatileRead will ensure volatility is dealt with.

If numberOfUpdates is defined as volatile int numberOfUpdates; you don't have to do this, as all reads of it will already be volatile reads.

There seems to be confusion about whether Interlocked.CompareExchange is more appropriate. Consider the following two excerpts from the documentation.

From the Thread.VolatileRead documentation:

Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.

From the Interlocked.CompareExchange documentation:

Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values.

In terms of the stated behavior of these methods, Thread.VolatileRead is clearly more appropriate. You do not want to compare numberOfUpdates to another value, and you do not want to replace its value. You want to read its value.

Lasse makes a good point in his comment: you might be better off using simple locking. When the other code wants to update numberOfUpdates it does something like the following.

lock (state)

When you want to read it, you do something like the following.

int value;
lock (state)
    value = state.numberOfUpdates;

This will ensure your requirements of atomicity and volatility without delving into more-obscure, relatively low-level multithreading primitives.

Read integers from console in Java, nextInt();. In the same way, take another input in a new variable. System.out.print( "Enter second integer: " ); Int  The nextInt() method, in Java, reads the next integer value from the console into the specified variable. Syntax: variableOfIntType = ScannerObject.nextInt(); where variableOfIntType is the variable in which the input value is to be stored.

How to Read and Print an Integer value in Java, nextInt(); where variableOfIntType is the variable in which the input value is to be stored. And ScannerObject is the beforehand created object of the Scanner class. Scanner class is in java.util package. It is used for capturing the input of the primitive types like int, double etc. and strings. Example: Program to read the number entered by user We have imported the package java.util.Scanner to use the Scanner.

Well, any value you read will always be somewhat stale as Hans Passant said. You can only control a guarantee that other shared values are consistent with the one you've just read using memory fences in the middle of code reading several shared values without locks (ie: are at the same degree of "staleness")

Fences also have the effect of defeating some compiler optimizations and reordering thus preventing unexpected behavior in release mode on different platforms.

Thread.VolatileRead will cause a full memory fence to be emitted so that no reads or writes can be reordered around your read of the int (in the method that's reading it). Obviously if you're only reading a single shared value (and you're not reading something else shared and the order and consistency of them both is important), then it may not seem necessary...

But I think that you will need it anyway to defeat some optimizations by the compiler or CPU so that you don't get the read more "stale" than necessary.

A dummy Interlocked.CompareExchange will do the same thing as Thread.VolatileRead (full fence and optimization defeating behavior).

There is a pattern followed in the framework used by CancellationTokenSource

//m_state uses the pattern "volatile int32 reads, with cmpxch writes" which is safe for updates and cannot suffer torn reads. 
private volatile int m_state;

public bool IsCancellationRequested
    get { return m_state >= NOTIFYING; } 

// ....
if (Interlocked.CompareExchange(ref m_state, NOTIFYING, NOT_CANCELED) == NOT_CANCELED) {
// ....

The volatile keyword has the effect of emitting a "half" fence. (ie: it blocks reads/writes from being moved before the read to it, and blocks reads/writes from being moved after the write to it).

Java Input and Output (I/O), hasNextInt()) { int number = input.nextInt(); System.out.println(number); } } }. This program opens a Scanner to read from the text file named "numbers.txt". To get input from keyboard, you can call methods of Scanner class. For example in following statment nextInt() method of Scanner takes an integer and returns to variable x : int x = console.nextInt(); Example : import java.util.Scanner; // Needed for Scanner class /** * This program demonstrates keyboard input.

Reading from files, Idiom #120 Read integer from stdin. Read an integer value from the standard input into variable n. Rust · Rust · C · Clojure · Cobol · C++ int n; scanf("%d", &n​);. To attribute some interpretation or meaning to something, especially an unintended meaning: He's reading things into the text that the author never intended. Don't read too much into her remark—it is hardly representative of her opinion.

Read integer from stdin, in Rust, You can convert data that is entered as a primitive type into a String by using the valueOf() method. int i = 10; String ten = String.valueOf ( i );. Basic Output. In Java​  What Is a Reading Model? Cindy remembers when she was in early elementary school and taught to read. They did a lot of worksheets and focused on phonics; kids in her class seemed to do fine with

Using Buffered Readers, Reads the next character from the standard input stream. public: static int Read();. C# Copy. offers 1,314 reading comprehension worksheets that are sure to help your students stay engaged in their reading, by providing interesting stories and effective exercises. We as teachers don’t always have the time to make up our own stories, or go through books to pick out excerpts to present in our lessons.

  • This seems to be a common question, see…. But I'm still not clear if Thread.VolatileRead or Interlocked.CompareExchange is better or if it doesn't matter.
  • One question worth considering: what happens next that relies on getting the 'latest' value of numberOfUpdates and how does this interact with whatever is incrementing it? What are the consequences if you read a slightly stale cached value?
  • Nothing. If I get a value that's old, it would be just fine, as long as I see the right value eventually. I'm not too worried about reading the int and getting a value that's 100 ms old. I'm more concerned about something being optimized or cached in a register, etc. so that my read never sees the updated value. I don't see how that could happen. But I just figured that I should read the int in the best, most idiomatic C# way possible. And I wasn't sure if the Interlocked.CompareExchange or Thread.VolatileRead was better or if it made no difference.
  • The best way is don't do it in the first place. Multiple threads is a bad idea. If you must have multiple threads, shared memory is a bad idea. If you must share memory, lock evasion is a bad idea. Put a lock around every single access to shared memory. If that turns out to be an unacceptable performance burden, and you can't eliminate contention, or the burden exists even with no contention, only then should you consider evaluating a low-lock solution.
  • I don't need the performance so there's no goal of low-lock code. Other developers are already modifying this integer using just Interlocked.* outside of any locks, so locks aren't an option for me. I just have to read this int in the best way possible, given that it's updated outside of locks (though always using Interlocked.* as far as I can see). If I was starting this from the beginning, I would just lock everywhere to make it simpler.
  • I agree with your sentiment about sticking with one synchronization primitive everywhere. And I've seen a lot of articles by people who really know what they're doing saying that volatile is confusing in terms of what it actually means. So my inclination is to stick with Interlocked for all reads. But I don't like that I have to "fake" it using Interlocked.CompareExchange(ref foo,0,0) instead of an Interlocked.Read(ref foo) that makes it clear from the code that all I want to do is read.
  • Actually, I just looked at the BCL for Interlocked.cs. And it looks like Interlocked.Read for longs is literally just implemented with Interlocked.CompareExchange(ref location,0,0). So I guess doing Interlocked.CompareExchange(ref foo,0,0) for ints should be just fine. If there was an int Interlocked.Read, that's how it would have been implemented.
  • @Michael +1 for checking the BCL. I've glanced at Interlocked.cs before, but never made it to the bottom of the page where Interlocked.Read is defined. I always assumed it had its own external implementation like most other members. Learn something new everyday.
  • This is good advice. Regarding volatile reads: a volatile read will give you the most up-to-date value available however the C# language makes no guarantees that a single canonical ordering of all volatile reads and writes will be observed by all threads. Remember, there is a big difference between "I have just read the latest value" and "that read of one variable will not be re-ordered with respect to a write of a different variable", but many people implicitly assume they are the same; they are absolutely not!
  • @markmnl Absolutely correct. It all depends on the memory model of CPU architecture. All Interlocked does is put memory fences around operations. On an x86 CPU (which has a strong memory model) this happens to result in the latest value being returned. Try using Interlocked on architectures with weaker memory models (e.g. ARM, PowerPC) and in certain scenarios unexpected things can happen.
  • But I think that Interlocked.Read() only works on longs? Not for ints.
  • It seems like there's two issues: preventing tearing and getting the real latest value. Interlocked.Read() will solve both (as I understand it) but only for longs. I don't have the tearing issue (regardless of x86 vs. x64) since I have an int. But I still have the second issue of reading in the latest value.
  • I want to make sure that I'm not reading in a cached value that's stale because the counter is being incremented on a different CPU.
  • @MichaelCovelli On x86 'Interlocked.CompareExchange' is implemented as 'CMPXCHG' with a full fence to prevent reordering. It will flush the store buffer but NOT the cache. The cache uses MESI to ensure that it doesn't read a dirty value. I'm not sure about other architectures.