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?

5
  • Please see here 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 and atomics. Commented Aug 8, 2019 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. Commented Aug 8, 2019 at 21:15
  • Well, you're probably better off avoiding volatile as much as possible, if you can. In this example I'd say it's better to only keep one variable volatile. Commented Aug 8, 2019 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? Commented Aug 8, 2019 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. Commented Aug 9, 2019 at 2:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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^^
cause i got the answer of that optimiziation here stackoverflow.com/questions/59376433/…, i can accept your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.