[concurrency-interest] Visibility of array initialization

Raph Frank raphfrk at gmail.com
Sun Aug 12 16:55:53 EDT 2018


On Sun, Aug 12, 2018 at 7:21 PM, Yuval Shavit <yankee.sierra at gmail.com>
wrote:

> I believe the value will be -1 or 2, but not 0 -- not for any special
> array semantics, but because the call to Thread::start introduces a
> happens-before (JLS 17.4.5).
>
> I think it could generate a zero since the 2nd array is a new array.  This
is potentially zero.


On Sun, Aug 12, 2018 at 8:54 PM, Valentin Kovalenko via
Concurrency-interest <concurrency-interest at cs.oswego.edu> wrote:

> Let's give letter names to some actions in executions of your program:
>

I tried to re-write based on your notation.


*Thread 1*
int[] array = new int[] {-1, -1, -1};  // INIT (Involves a W0, WE and WA
but is all synced by thread2.start() anyway)

thread2.start();
// This acts as the point of forking for the 2 threads.

......

array = new int[] {1, 2, 3};
//  Based on your comments, this line can be split
//
// W0:  set all elements to zeros (time zero operation)
//
// WE:  Initialize [2] element to 3
// WA:  Set array to reference the new array


*Thread 2*
System.out.println(array[2]);

INIT happens before everything, since it happens before the .start() call
of thread 2.

W0 happens before the System.out.println call, since it is default
initialization.

WE & WA have no happens before relationship with the System.out call.

This means that the System.out could see WE or WA.  If it doesn't see
either of them, then it would see the result of W(0).

This means that 3, 0 and -1 are possible.  Is this correct?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/a8ef2df6/attachment.html>


More information about the Concurrency-interest mailing list