1

the following problem:

volatile Object A;
volatile Object B;
volatile Object C;

Thread1:
reads and writes to A-C 

Thread2:
the same as Thread1

So my question is: would it better if i do something like this:

Object A;
Object B;
Object C;
volatile boolean memoryBarrier=true;

Thread1:
Before read of A-C: read memoryBarrier
After some write of A-C: write memoryBarrier=true;

Thread2:
the same as Thread1:
Before read of A-C: read memoryBarrier
After some write of A-C: write memoryBarrier=true;

Is that better having only one volatile variable, or should i make each variable i could write/read on valatile?

And is that ok, if i write each time true to my memoryBarrier in my secound solution?, to trigger the write-read- happens before relationsship semantic in java? I guess its not optimezed away?

So the summary: Are my solution 1 and 2 semantically the equal? Is solution 2 better? Can i always write the same value a volatile variable to get read/write volatile happensbefore-relationsship?

Robin Kreuzer
  • 162
  • 1
  • 10
  • Please see [here](https://stackoverflow.com/questions/106591/what-is-the-volatile-keyword-useful-for?rq=1) for the workings of the volatile keyword. With regards to the question I think you would be honestly better off using some of the concurrency primitives recently implemented in Java, like [locks](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/package-summary.html) and [atomics](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html). – Avi Aug 08 '19 at 21:12
  • i know how volatile works, in my question i didnt asked how volatile work, i asked, if it is better to make all variables, i want to see in other threads, volatile or if it is better (for exampel for performance reason) making only one variable volatile. – Robin Kreuzer Aug 08 '19 at 21:15
  • Well, you're probably better off [avoiding](https://stackoverflow.com/a/11133187/5699679) volatile as much as possible, if you can. In this example I'd say it's better to only keep one variable volatile. – Avi Aug 08 '19 at 21:26
  • @Avi the reordering thing could it really slow down, my thirst solution; so you would each time use my second sulotion? That means i create always a volatile memoryBarrier variable, if i have two or more other variables, that i want to access in a way that i want to see their content, if it has change?..... That would be a good solution? – Robin Kreuzer Aug 08 '19 at 22:33
  • 1
    @RobinKreuzer Like I said, if you must use volatile, I would only use one such variable. However, I personally try to avoid using volatile at all, by using other concurrency primitives to control access to shared data members. – Avi Aug 09 '19 at 02:09

1 Answers1

1

The example is so trivial so you might not see much of a difference performance-wise.

My intuition tells me that having 3 non-volatile memory accesses followed by a single volatile access is probably better than issuing 3 volatile accesses in a row.

Those three volatile memory accesses are totally ordered (A happens-before B happens-before C) and restricts the compiler and processor from performing some optimizations. The non-volatile version establishes no happens-before relation between A, B, and C, and therefore give the compiler and processor more freedom to exploit memory-level parallelism/instruction-level parallelism.

Eric
  • 906
  • 7
  • 13
  • can you add to your answer a answer of the following: And is that ok, if i write each time true to my memoryBarrier in my secound solution?, to trigger the write-read- happens before relationsship semantic in java? I guess its not optimezed away? then i can accept your answer^^ – Robin Kreuzer Sep 22 '19 at 23:55
  • cause i got the answer of that optimiziation here https://stackoverflow.com/questions/59376433/one-java-memoryflushing-volatile-a-good-programdesign, i can accept your answer – Robin Kreuzer Dec 17 '19 at 16:54