Question of the Day

C++ Question of the Day: Volatile Keyword

Jon asks:

Do you remember from somewhere(possibly a Sutter talk?) what happens
in the below?

volatile int x;

Thread #1:
   Write 1 to x
   Read x

Thread #2:
   Write 2 to x
   Read x

Yes, I do remember the talk but am not sure who it was by. First, let's start with what the volatile keyword does.

volatile
"The volatile qualifier tells the compiler to avoid certain optimizations because the object's value can change in unexpected ways" (C++ In A Nutshell, pg 325)
Syndicate content