+ 1
What is the use of volatile and mutable keyword plzz explain it with examples...??
2 Answers
+ 4
volatile tells the compiler that this variable can be changed by other threads or hardware so that the compiler makes no unwanted optimizations !
eg.
static volatile int status;
void poll_status( void )
{
status = 0;
while ( status == 0 );
}
in this example the compiler would optimize the variable "status" out and would replace it with true leaving a endless loop and making it impossible to get out of it by changing "status" in a other thread. by saying status is volatile the compiler leaves the variable alone and it can be altered by other threads ... in this example we " busy" wait for a thread to change " status"
+ 1
for mutable look at this stackoverflow answer
https://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to