3

I have a class ContainerClass that has some static variables. Several simultaneously running threads access these static variables and they always need to have the most recent value.

The threads access the variables without an object of the ContainerClass, but instead like

ContainerClass.variable_A;

Do I still need to declare the variables that are shared between the threads volatile in ContainerClass? Does any caching happen in the threads?

EDIT: Edited for some clarity.

EDIT2: For more clarity: Multiple threads read the values of these volatile variables, but only one thread sets them. Will the reading-threads cache the variable or always have the up-to-date version, since there's no object instantiation in the reading threads?

spacecoyote
  • 1,909
  • 3
  • 19
  • 24

2 Answers2

4

Quick answer is Yes. Static is a instance/class modifier. It has nothing to do with synchronization and memory model. While volatile ensures 1) instruction optimization doesn't swap the operation to volatile variable to anything before/after that 2)anything happens before manipulating the volatile variable are written to main memory. So yes you do need to make it volatile.

Having said all the above I personally think your design isn't good. Exposing such global static volatile basically violates encapsulation. Unless you have such a performance reason to do so (which I guess not), the static var better be private member and latest value returned by methods to have better encapsulation.

Alex Suo
  • 2,977
  • 1
  • 14
  • 22
  • `static` is not a visibility modifier. It is so-called `instance/class` modifier. Your 2) statement about volatile variables is also incorrect, it ensures that writing something to volatile variable happens-before this value is read. – Alexey Malev Apr 15 '14 at 10:55
  • Surely takes your comment on static coz I typed too quick. While I don't quite agree on volatile. It ensures writing something to other variables happens-before the volatile value is read for the same thread while volatile guarantees only latest value across threads but nothing is guaranteed on read-write sequence. – Alex Suo Apr 15 '14 at 11:10
  • `volatile` instructions cannot be reordered with nonvolatile, this provides us guarantees of visibility of values written there, but only **after** volatile read which happens-after this volatile variable has been written (maybe a little bit incorrect English, sorry). To achieve this guarantees, nonvolatile write should happens-before volatile write. – Alexey Malev Apr 15 '14 at 11:14
  • OK so we basically have the same point on volatile here. Just your first comment reads "writing something to volatile variable happens-before this value is read". I think you mean "something to other variables" instead of "volatile variable". – Alex Suo Apr 15 '14 at 11:16
  • No, I meant that writing value to volatile variable always happens-before this value is read "from" this variable. My previous comment is about relation with R/W of other variables. Hope we got to same point now :) – Alexey Malev Apr 15 '14 at 11:17
  • OK I see your point and I agree. Just simply the operations on volatiles won't be reordered. Good same point :) – Alex Suo Apr 15 '14 at 11:21
  • Do you mind if I ask you to update your post about `static` and visibility not to confuse the OP and other people who don't want to read comments? – Alexey Malev Apr 15 '14 at 11:23
  • Thanks for the responses. This was very confusing but I think the main message is that applying changes to these volatile variables still needs to happen in a synchronized function. I'm doing that right now. About the "bad" design, I actually did pick this for performance reasons, and the multiple threads actually only retrieve the value of the volatile variables. Only one other thread will change the values of the volatiles. To be honest I'm still not sure if ContainerClass.variable_1 will always return the up-to-date value or a cached version to the other threads that read it. – spacecoyote Apr 15 '14 at 18:56
1

The exact answer to your quesion is It depends. If these variables change values which I guess is intended, and there is no additional synchronization performed - then yes, you need something to provide visibility guarantees for values of these variables because static modifier itself has nothing coming with it about visibility. volatile can be your choice.

Just in case your variables are not changed, making them final is also enough for visibility guarantees.

Alexey Malev
  • 6,408
  • 4
  • 34
  • 52