I have a shared variable of type double
. This variable will be accessed by two threads. One thread will ever only write the variable, whereas the other thread will ever only read the variable.
Do I still get race condition here? If yes, is there an "easy" way in C++ to implement atomic access? How do I implement it efficiently if there is going to be much much more reads than writes? Do I need to mark the variable as volatile
?
EDIT: OK the "reader" thread works periodically on batches of data and the propagation of the new value is not time-sensitive. Instead of implementing complicated interlocking that I have no good way to test, I can just declare another temp variable the writer thread will write to. Then when the reader is finished with one batch, it can atomically propagate the temp value to the actual variable. Would that be race-condition-free?