Volatile and Cross-Thread Variable Changes #78346
Replies: 4 comments
-
Generally speaking, i view the correct way as using a normal 'lock'. If there are exceptional circumstances that do not allow for that, then the answer will depend on those circumstances. |
Beta Was this translation helpful? Give feedback.
-
Unfortunate historical reasons. The compiler does not use that API at all. C#'s AFAIK, there's no technical reason why the compiler has to continue to emit |
Beta Was this translation helpful? Give feedback.
-
Thanks so much for the answers here. With my code, I want all threads to always be able to read the current version of the variables (ideally, without a lock). When updating the variables, I do lock. Will a volatile variable that has a Lock around all writes guarantee that all threads always see the current value? |
Beta Was this translation helpful? Give feedback.
-
This question is not new :) https://stackoverflow.com/questions/4727068/why-not-volatile-on-system-double-and-system-long |
Beta Was this translation helpful? Give feedback.
-
What is the correct way to ensure that variables changed on one thread are observed on another?
Also, why is it that a
long
cannot be declared as volatile but you canVolatile.Read/Write
longs? I expected that the API would be consistent with the keyword.Beta Was this translation helpful? Give feedback.
All reactions