0

In Java, AtomicInteger and AtomicLong seem to implement the same functionality as .NET's Interlocked. Additionally, they provide a get() method to read the most current value (which may differ from that stored in the CPU cache):

    private volatile int value;

    // ...

    /**
     * Gets the current value.
     *
     * @return the current value
     */
    public final int get() {
        return value;
    }

On the other hand, Interlocked only provides a Read(...) method for 64-bit values, which only guarantees they're read consistently (i.e. on 32-bit system you don't read any garbage), but not that the value read is the most current one.

Now the question. If I use a volatile variable in conjunction with Interlocked, does it guarantee that the value can be both incremented/decremented and read/written atomically?

Bass
  • 4,977
  • 2
  • 36
  • 82
  • 1
    possible duplicate of [Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?](http://stackoverflow.com/questions/2475222/does-interlocked-guarantee-visibility-to-other-threads-in-c-sharp-or-do-i-still) – itsme86 Jul 07 '14 at 20:30
  • 1
    Indeed. This looks like the valid answer to my question. I wonder how comes I missed it earlier. – Bass Jul 07 '14 at 20:39

0 Answers0