[concurrency-interest] Concurrent Read/Write without Synchronization

David Holmes davidcholmes at aapt.net.au
Fri Dec 17 23:20:02 EST 2010


A thread need only see a write performed by another thread if there is a
happens-before relationship between the read and the write. Without that
various things can happen at the both the VM/JIT level and at the hardware
level.

The JIT can transform code such that a field is not read because its value
is not written in the current code eg:

   class Service {
      boolean stopped = false;
     void stop() { stopped = true};
     void run() {
        while (!stopped) doStuff();
     }
  }

the JIT can transform run() as if it were written:

   if (!stopped)
      while(true) doStuff();

At the hardware level stores/writes can languish in buffers longer than
might be expected and stores and loads (reads and writes) cam be reordered
in surprising ways.

Without the right synchronization actions to enforce the happens-before
ordering, there are no guarantees of the kind your question asks.

David Holmes
  -----Original Message-----
  From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Insane
Membrane
  Sent: Saturday, 18 December 2010 12:29 PM
  To: concurrency-interest at cs.oswego.edu
  Subject: [concurrency-interest] Concurrent Read/Write without
Synchronization


  Hi-


  Could someone please answer this simple question for me? I have two
threads, A and B, and they share an object P that has an integer property
called x.  The first question is, if A sets x to a value, but B only ever
reads x, will B ALWAYS see the latest value of x?  So A does x = x + 1.
Will B always see x with the new value? We were under the impression the
latest value could still be sitting in the cache of A, but not visible in B.
This would be one reason for the volatile or interlocked operations?
Similarly, if we have an object reference and we set it onto some
public/global reference, all threads would always see whatever had been
written to this global reference IMMEDIATELY, without any needs for
synchronization or interlocked?


  This is assuming the int is 32bit on a 32bit machine, and the reference is
the same too.


  Thank you,


  mf.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20101218/79df7659/attachment-0001.html>


More information about the Concurrency-interest mailing list