Hi,
One of the posts in this very interesting threat referred to this article
:
http://msdn2.microsoft.com:80/en-us/library/ms686355.aspx
This article includes the following piece of code (to fix the race
condition):
volatile int iValue;
volatile BOOL fValueHasBeenComputed = FALSE;
extern int ComputeValue();
void CacheComputedValue()
{
if (!fValueHasBeenComputed)
{
iValue = ComputeValue();
fValueHasBeenComputed = TRUE;
}
}
BOOL FetchComputedValue(int *piResult)
{
if (fValueHasBeenComputed)
{
*piResult = iValue;
return TRUE;
}
else return FALSE;
}
My (complete ignorent) question is this:
Must fValueHasBeenComputed really be declared volatile for the program to
produce correct results ? Isn't it true that , because iValue is declared
volatile, it is impossible that fValueHasBeenComputed = TRUE and the
result of ComputeValue() is not stored in iValue ?
Of course, because fValueHasBeenComputed is not declared volatile it may
be that FetchComputedValue() returns FALSE when in fact iValue does
contain the computed value but the other case can never occur. E.g.
FetchComputedValue returns TRUE and piResult is incorrect.
Are my assumptions correct ?