[concurrency-interest] Racy lazy initialization: blush test

Aleksey Shipilev aleksey.shipilev at oracle.com
Sat Feb 16 05:42:42 EST 2013


Hi there,

I think it's time to have another argument about racy stuff! :)

I want to assign the grading for some of the new
java-concurrency-torture tests, and wanted to cross-check the reasoning.
Given this test [1], what are the plausible outcomes?

In pseudo-code:

   class X {
       int f;
   }

   X x = new X();

          T1      |     T2
   ---------------+---------------
   if (x.f == 0)  | if (x.f == 0)
       x.f = 1    |     x.f = 1
   r1 = x.f       | r2 = x.f

Is there a valid execution which yields (r1, r2) as (1, 0) or (0, 1)?
(It appears there are valid executions under JMM, mostly because there
is the data race on x.f; Jeremy had a post [2] about the similar issue
before, and reaching to the same conclusion.)

In that spirit,

          T1        |      T2
   -----------------+---------------
   rX1 = x.f        | rX2 = x.f
   if (rX1 == 0) {  | if (rX2 == 0) {
       rX1 = 1      |     rX2 = 1
       x.f = rX1    |     x.f = rX2
   }                | }
   r1 = rX1         | r2 = rX2

...should always yield (r1, r2) = (1, 1).

Thanks,
Aleksey.

[1]
https://github.com/shipilev/java-concurrency-torture/blob/master/src/main/java/org/openjdk/concurrent/torture/tests/causality/lazyinit/plain/IntLazyTest.java
[2] http://jeremymanson.blogspot.ru/2008/12/benign-data-races-in-java.html


More information about the Concurrency-interest mailing list