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.