Elastic interview question

How does volatile work?

Interview Answer

Anonymous

Jul 22, 2018

Volatile works by guaranty no reordering of reading and write operations will be done and all read and write operations will be done on the main memory, not CPU cache, thus ensuring that if a single thread is writing/read and others are only read they will all see the same value. This is applicable only when a single writer is used. If multiple threads change the same variable (memory) it is not guaranteed a consistent view of values.