Volatile

volatile: The Lie (1)

What volatile does:

  • Prevents compiler optimization of everything involving the variable declared volatile

  • Corollary: the variable must not be kept in a register

volatile int x;

Attention:

  • All it does is provide a false impression of correctness

  • Most of its uses are outright bugs

volatile: The Lie (2)

What volatile doesn’t:

  • Variable can still be in a cache

    • Variable is not at all sync with memory when using write-back cache strategy

  • Not a memory barrier ⟶ load/store reordering still possible (done by CPU, not by compiler)

  • Not a replacement for proper locking

Still broken: load-modify-store

volatile int use_count;

void use_resource(void)
{
  do_something_with_shared_resource();
  use_count++;
}

volatile: Valid Use: Hardware

Originally conceived for use with hardware registers

  • Optimizing compiler would wreak havoc

    • Loops would never terminate

    • Memory locations would not be written to/read from

volatile int completion_flag;
volatile int out_word;
volatile int in_word;

int communicate(int word)
{
    out_word = word;
    while (!completion_flag);
    return in_word;
}

volatile: Valid Use: Unix Signal Handlers

A variable might change in unforeseeable ways

  • Signal handler modifies quit variable

  • Optimizing compiler would otherwise make the loop infinite

volatile int quit;

int main(void)
{
  while (!quit)
    do_something();
}