Volatile piggyback. Is this enough for visiblity?

后端 未结 5 882
梦谈多话
梦谈多话 2020-12-05 00:45

This is about volatile piggyback. Purpose: I want to reach a lightweight vars visibilty. Consistency of a_b_c is not important. I have a bunch of vars and I don\'t want to m

5条回答
  •  执笔经年
    2020-12-05 01:26

    Java Memory Model defines the happens-before relationship which has the following properties (amongst others):

    • "Each action in a thread happens-before every action in that thread that comes later in the program order" (program order rule)
    • "A write to a volatile field happens-before every subsequent read of that same volatile" (volatile variable rule)

    These two properties together with transitivity of the happens-before relationship imply the visibility guarantees that OP seeks in the following manner:

    1. A write to a in thread 1 happens-before a write to sync in a call to sync() in thread 1 (program order rule).
    2. The write to sync in the call to sync() in thread 1 happens-before a read to sync in a call to sync in thread 2 (volatile variable rule).
    3. The read from sync in the call to sync() in thread 2 happens-before a read from a in thread 2 (program order rule).

    This implies that the answer to the question is yes, i.e. the call to sync() in each iteration in threads 1 and 2 ensures visibility of changes to a, b and c to the other thread(s). Note that this ensures visibility only. No mutual exclusion guarantees exist and hence all invariants binding a, b and c may be violated.

    See also Java theory and practice: Fixing the Java Memory Model, Part 2. In particular the section "New guarantees for volatile" which says

    Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are guaranteed now to be visible to B.

提交回复
热议问题