+ 1

What is the use of volatile and mutable keyword plzz explain it with examples...??

13th Feb 2018, 3:35 PM
Amigo X-boy
Amigo X-boy - avatar
2 Réponses
+ 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"
13th Feb 2018, 4:02 PM
Chrizzhigh
Chrizzhigh - avatar
13th Feb 2018, 4:07 PM
Chrizzhigh
Chrizzhigh - avatar