I know about volatile
variables that are defined at file scope. The compiler isn't allowed to make assumptions about these variables. They may be changed at nearly any time and the compile must not optimize out reads of the variable.
Now I found this code
BOOL InstallHandler()
{
volatile BOOL b_bulk_erase = FALSE;
volatile BOOL b_test_read_write = FALSE;
volatile BOOL b_continue = TRUE;
...
if (b_test_read_write)
{
read();
write();
}
}
How does the volatile
corresponds to variables to the stack i.e. owned by one thread?
Edit:
With owned by one thread I wanted to express that the variable is not exposed. The address isn't given to anything else. It is not used by any other thread.
Ok, I realized the intention for that volatile.
The function is part of a device driver that communicates with a hardware device. The functions read
and write
are not used during normal operation.
But when the developer runs the program in a debugger she can set a breakpoint at the if
clause and change the variable with the debugger. This will allow to manipulate the execution and to call to read
and write
.
The volatile
inhibits the optimization that would be possible since the if
clause tests a const expression. Without the volatile
the whole if
probably wouldn't be in the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With