I just can't help keeping thinking about a stupid irrelevant question.
Say this piece of code is in a source file and we want to keep variable diff visible to this file only, so float diff becomes static float diff.
Is it possible, between line diff += myDiff and line if (diff/(n*n)) < TOLERANCE), the compiler is unaware of the fact that value diff could have been modified by other threads, so instead of re-loading diff from memory, the compiler reuses diff from the register (as in line diff += myDiff value diff is already in register and no code between the two lines changes it), and we'll have to make diffvolatile static float diff?
kayvonf
@Sherry. This is an excellent (and advanced) observation. It's certainly not stupid.
The short answer is that a properly written synchronization library will include the appropriate operations to make sure thread-local compiler optimizations like register allocation won't result in incorrect multi-threaded behavior in such situations.
I just can't help keeping thinking about a stupid irrelevant question.
Say this piece of code is in a source file and we want to keep variable
diff
visible to this file only, sofloat diff
becomesstatic float diff
.Is it possible, between line
diff += myDiff
and lineif (diff/(n*n)) < TOLERANCE)
, the compiler is unaware of the fact that valuediff
could have been modified by other threads, so instead of re-loadingdiff
from memory, the compiler reusesdiff
from the register (as in linediff += myDiff
valuediff
is already in register and no code between the two lines changes it), and we'll have to makediff
volatile static float diff
?@Sherry. This is an excellent (and advanced) observation. It's certainly not stupid.
First of all, you may be interested in this conversation from two years ago: http://15418.courses.cs.cmu.edu/spring2013/lecture/progmodels/slide_019
Second, you should read this: http://stackoverflow.com/questions/6837699/are-mutex-lock-functions-sufficient-without-volatile
The short answer is that a properly written synchronization library will include the appropriate operations to make sure thread-local compiler optimizations like register allocation won't result in incorrect multi-threaded behavior in such situations.