Volatile Fields

Multithreaded programming is notoriously hard, especially when optimizations are carried out by the compiler, runtime, or even the hardware. In particular, reads and writes to memory can sometimes be reordered to improve execution efficiency. In such a case, fields can be declared as volatile to ensure reads and writes to the field are not moved relative to surrounding code. Because this is a very specialized subject, refer to online documentation detailing the CLR memory model and practical uses of the volatile keyword.

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.