happens-before

Java memory model: volatile variables and happens-before

假装没事ソ 提交于 2019-12-17 09:19:11
问题 I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are the following statements correct in accordance with Java memory model (JMM)? If not, what would be correct interpretation? i = 1 always happens-before v = 2 v = 2 happens-before vDst = v in JMM only if it's actually happens before in time i = 1 happens

Out of order writes without memory-barrier: the only possible cause of Data Race?

…衆ロ難τιáo~ 提交于 2019-12-12 15:58:45
问题 While going through Java Concurrency in practice by Brian Goetz I encountered the following line: A data race occurs when a variable is read by more than one thread, and written by at least one thread, but the reads and writes are not ordered by happens-before . A correctly synchronized program is one with no data races; correctly synchronized programs exhibit sequential consistency, meaning that all actions within the program appear to happen in a fixed, global order. My Question is that, Is

Is happens-before relation given in case of invokeLater() or invokeAndWait?

人盡茶涼 提交于 2019-12-12 15:21:24
问题 Pretty sure it is this way - but I like to know for sure - is happens-before relation given in case of invokeLater() or invokeAndWait()? The methods are defined in (SwingUtilities respectively) AWT.EventQueue. I guess there is synchronization involved when something is entered in the EventQueue and hence as result of the synchronization, the happens-before relation and finally the visibility is given. But is it really that way? (and where can I find that information?) e.g. inside some worker

What does “happens before” mean in the C++11 spec?

柔情痞子 提交于 2019-12-11 10:09:06
问题 I'm trying to understand the meaning of happens before in the C++11 spec, and in particular whether the spec assumes any informal understanding of the term in addition to what is specified. I'm working from draft N3290. A straight-forward argument that the term should be interpreted only with respect to the specification itself is that the spec actually talks about its own definition of the term. For example, 1.10.6: "happens before (as defined below)" or 1.10.11: "'happens before' relation,

With double-checked locking, does a put to a volatile ConcurrentHashMap have happens-before guarantee?

孤人 提交于 2019-12-11 04:24:03
问题 So far, I have used double-checked locking as follows: class Example { static Object o; volatile static boolean setupDone; private Example() { /* private constructor */ } getInstance() { if(!setupDone) { synchronized(Example.class) { if(/*still*/ !setupDone) { o = new String("typically a more complicated operation"); setupDone = true; } } } return o; } }// end of class Now, because we have groups of threads that all share this class, we changed the boolean to a ConcurrentHashMap as follows:

Java: how volatile guarantee visibility of “data” in this piece of code?

两盒软妹~` 提交于 2019-12-06 15:06:19
问题 Class Future { private volatile boolean ready; private Object data; public Object get() { if(!ready) return null; return data; } public synchronized void setOnce(Object o) { if(ready) throw...; data = o; ready = true; } } It said "if a thread reads data, there is a happens-before edge from write to read of that guarantees visibility of data" I know from my learning: volatile ensures that every read/write will be in the memory instead of only in cache or registers; volatile ensures reorder:

What does “strongly happens before” mean?

做~自己de王妃 提交于 2019-12-05 22:09:00
问题 The phrase "strongly happens before" is used several times in the C++ draft standard. For example: Termination [basic.start.term]/5 If the completion of the initialization of an object with static storage duration strongly happens before a call to std​::​atexit (see , [support.start.term]), the call to the function passed to std​::​atexit is sequenced before the call to the destructor for the object. If a call to std​::​atexit strongly happens before the completion of the initialization of an

Java: how volatile guarantee visibility of “data” in this piece of code?

本秂侑毒 提交于 2019-12-04 18:56:36
Class Future { private volatile boolean ready; private Object data; public Object get() { if(!ready) return null; return data; } public synchronized void setOnce(Object o) { if(ready) throw...; data = o; ready = true; } } It said "if a thread reads data, there is a happens-before edge from write to read of that guarantees visibility of data" I know from my learning: volatile ensures that every read/write will be in the memory instead of only in cache or registers; volatile ensures reorder: that is, in setOnce() method data = o can only be scheduled after if(ready) throw..., and before ready =

Java synchronized and happens before [duplicate]

ぐ巨炮叔叔 提交于 2019-12-02 16:11:24
问题 This question already has answers here : How to understand happens-before consistent (4 answers) Closed last year . A synchronized statement establishes a happens-before relation. But im not sure about the details. In http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html one can read An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor I want to know if i

Java synchronized and happens before [duplicate]

十年热恋 提交于 2019-12-02 10:14:01
This question already has an answer here: How to understand happens-before consistent 4 answers A synchronized statement establishes a happens-before relation. But im not sure about the details. In http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-summary.html one can read An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor I want to know if i understood that correctly. Therefore have a look at the following example. Lets assume that there are 2 Threads T1,T2 sharing the